I'm currently working on getting the hang of Core Data, so I'll be updating this as I learn. To start off, I'm reading these links (in order):
- Developing with Core Data
- Introduction to Core Data Programming Guide
- Introduction to Low-Level Core Data Tutorial
- Introduction to Core Data Model Versioning and Data Migration Programming Guide
- Creating a Managed Object Model with Xcode
- Core Data Framework Reference
- Video Tutorials on creating a basic Core Data Document-based app
- A list of Core Data articles on hyperjeff.net
- A great overview of the classes that make Core Data work
With each major release of Mac OS X, Apple has been adding more features to the Cocoa Frameworks that simplify the development process. From the start, everything has been about the Model-View-Controller design paradigm. The first to get implement was the View aspect, in the form of Interface Builder (IB). IB is basically a drag-and-drop interface construction kit. With it, developers can rapidly create professional looking interfaces without having to deal with things like JButtons or JPanels that are so proliferate in Java.
Panther (10.3) introduced things on the controller level: data-specific controllers. These are things such as
NSWindowController, and so on. These objects provided simple ways to connect stores of information to the interface.
About the same time, Cocoa Bindings came along. Bindings are a fantastic way to get rid of almost all the "glue code" that resides in controller objects. This glue code is what ties the model (the data structure) to the view (the interface). With Bindings, apps suddenly became much lighter and developers could concentrate more on the things that made their apps unique.
In all of this, though, Apple was virtually ignoring the Model layer. The View and Controller components all had their niceties, but the model, which is arguably the most important part of any application, was still left up to the developer to code by hand. To remedy this, Apple introduced Core Data in Tiger (10.4).
What is Core Data?
Core Data is for the Model what Interface Builder is for the View. It's a graphical way to define how data is stored internally and how different kinds of data relate to each other.
To create an application with Core Data, choose File ▸ New Project... (⇧⌘N) and select "Core Data Application", "Core Data Document-based Application", or "Core Data Document-based Application with Spotlight Importer".
Choosing one of these options will include all of the necessary code to load your app's data file. If you want to add Core Data support to an existing app, the following code is the basic outline for how to define where Core Data stores your Model:
NSURL *url = [NSURL fileURLWithPath:@"path/to/file.xml"]; NSArray *bundles = [NSArray arrayWithObject:[NSBundle mainBundle]]; NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:bundles]; NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; [coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url error:&error]; NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator: coordinator];