Core Data is an object graph and persistence framework provided by Apple in the Mac OS X and iOS operating systems. It’s been around for quite a while (since iPhone SDK 3.0) and it’s probably one of the most misunderstood Frameworks on OS X and iOS. After my first experience with it I can totally understand the love/hate relationship most developers have with it.
There are a lot of good examples on how to setup your app to start using Core Data quickly and painfully, and I absolutely recommend lessons 12 and 13 from Developing iOS 7 Apps by Stanford University, but the problem is that most of them are specific to iOS and even the ones that are not (e.g the one at raywenderlich.com) don’t go very deep in detail as they just show basic code-free Cocoa Bindings stuff (see the link above).
The problem is that while Core Data for OS X and Core Data for iOS are basically the same thing 90% of the time, the differences that lie in that remaing 10% will make you want to smash your beloved Macbook on the floor, multiple times.
On iOS all you have to do to start enjoying all the cool stuff about Core Data is create an instance of UIManagedDocument. This will automatically generate the Persistent Store Coordinator, the Persistent Object Store and, most importantly, the Managed Object Context (MOC from here on).
Because if you’re in a hurry and you just want to jump in and skip to the good stuff, all you need to know is where your MOC’s at.
I’m not going to explain from scratch how Core Data works (objc.io does a great job doing that in its Core Data Overview) but basically you define entities (and their attributes and relationships) in your Data Model, then you insert instances of those entities into your MOC and set their attributes. Upon calling
[moc save:&error] you save those instances and the changes you’ve made to them in your Persistent Store (a wrapper for the actual database file in the filesystem). This means that before that call you have full undo/redo support for the changes you (and your app’s user) made on the moc. On iOS, you don’t even have to call that
save method because UIManagedDocument takes care of everything for you. Cool, right?
The problem is that there’s no such thing as UIManagedDocument (not even a close relative) on OS X, so you have to take care of generating all that stuff I was telling you about on your own.
The best way you can do that is by starting a new Xcode project and choose “Use Core Data” option. This will generate the necessary code in you Application Delegate. If you can’t start a new project, just copy all the Core Data related methods from a new project’s AppDelegate in your own.
Now all you need to know to use Core Data is, as I previously said, where your Managed Object Context’s at. The MOC has been created by the code you just copied in your AppDelegate so just get a pointer to that from whenever you are in your application by doing
AppDelegate *delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
and then access its moc property like this
NSManagedObjectContext *moc = delegate.managedObjectContext;
That’s it, you’re all ready to go. Happy coding!
Leave a Reply