The initial structure of my app ChronoCat was super simple: Just one custom class, all objects from that class are thrown into an array, that array is displayed by a table view and is written/read from disk as needed. Here is the basic process I used to migrate the app away from objects-in-an-array to Core Data:
- Make a Core Data entity to represent my custom class.
- Added all the @propertys from my custom class as attributes to the new entity. (@propertys only at this point, methods come later) .
- Make the NSManagedObject Subclass of my new Core Data model.
- Make a category for my NSManagedObject Subclass to store any methods I had in my custom class.
Now that the swap from a custom object to Core Data was complete the last step was to create a class to import the user’s existing data into the new Core Data model. Here is what I did for that:
- Made a new group in Xcode and added the old custom class used in the initial version of ChronoCat to it. The new version of my app is a complete rewrite so the old class file was not already in the project.
- In that same group I added a new class to put all my import code in.
- The import process is to check for the old data file in the app’s document folder, if found load the data from binary back into an NSArray, then use a for loop to cycle through the objects in the array and save them as a new NSManagedObjects.
- Once that’s done clean up by deleting the old data file.
The whole process was shockingly painless and has been a great way to get my feet wet with Core Data. The new version of ChronoCat that utilizes Core Data is currently in review and will hopefully be approved and available soon.