This chapter shows you how to create and configure the Core Data stack, from the managed object context to the underlying persistent store.
The managed object context is responsible for managing a graph of managed objects. The task of managing the persistent stores falls to the persistent store coordinator. Its job is to mediate between the managed object context or contexts and the persistent store or stores. It presents a façade to the contexts, representing a collection of stores as a single virtual store. In this example, the coordinator manages just a single store.
To add a store, you use the NSPersistentStoreCoordinator
method addPersistentStoreWithType:configuration:URL:options:error:
. This returns an object representing the new store (or nil
if it cannot be created). You must specify both the store’s location in the file system and its type (this example does not make use of model configurations). In this example it is an XML store—because its reasonably human-readable form facilitates testing. The file name extension is not .xml
. You should avoid using generic file extensions—consider what would happen if all applications used the same extension…
The managedObjectContext
function returns a fully configured managed object context. It also creates and configures the remainder of the Core Data stack if necessary.
The first step is to determine whether the managed object context instance already exists. If it does, simply return it, if it doesn’t, create it and then configure the remainder of the stack.
At the top of the main source file, before main
add a declaration for the function NSManagedObjectContext *managedObjectContext()
.
In the main source file, implement the managedObjectContext
function. Declare a static variable for the context. If the variable is not nil
return it immediately. If it is nil
, create a new context, then return it as the function result.
NSManagedObjectContext *managedObjectContext() |
{ |
static NSManagedObjectContext *moc = nil; |
if (moc != nil) { |
return moc; |
} |
moc = [[NSManagedObjectContext alloc] init]; |
// implementation continues... |
return moc; |
} |
The second main step is to create the persistent store coordinator and configure the persistent store. You also need to tell the context that it should use this new coordinator.
Create a persistent store coordinator, then set the coordinator for the context.
NSPersistentStoreCoordinator *coordinator = |
[[NSPersistentStoreCoordinator alloc] |
                initWithManagedObjectModel:managedObjectModel()]; |
[moc setPersistentStoreCoordinator: coordinator]; |
Create a new persistent store of the appropriate type. If for some reason the store cannot be created, log an appropriate warning.
NSString *STORE_TYPE = NSXMLStoreType; |
NSString *STORE_FILENAME = @"CDCLI.cdcli"; |
NSError *error = nil; |
NSURL *url = [applicationLogDirectory() URLByAppendingPathComponent:STORE_FILENAME]; |
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE |
configuration:nil |
URL:url |
options:nil |
error:&error]; |
if (newStore == nil) { |
NSLog(@"Store Configuration Failure\n%@", |
([error localizedDescription] != nil) ? |
[error localizedDescription] : @"Unknown Error"); |
} |
So that you can test the implementation thus far, instantiate the managed object context.
In the main
function, after the line in which the description of the managed object model is logged, declare a variable of type NSManagedObjectContext
and assign its value to the result of invoking the managedObjectContext
function.
NSManagedObjectContext *moc = managedObjectContext(); |
Build and run the utility. It should compile without errors, although you should get a warning that the variable moc
is unused in the main
function. When you run the utility, the managedObjectContext
function should not log any errors.
The complete listing of the managedObjectContext
function is shown in Listing 5-1.
Listing 5-1 Complete listing of the managedObjectContext
function
NSManagedObjectContext *managedObjectContext() { |
static NSManagedObjectContext *moc = nil; |
if (moc != nil) { |
return moc; |
} |
moc = [[NSManagedObjectContext alloc] init]; |
NSPersistentStoreCoordinator *coordinator = |
[[NSPersistentStoreCoordinator alloc] |
                initWithManagedObjectModel: managedObjectModel()]; |
[moc setPersistentStoreCoordinator: coordinator]; |
NSString *STORE_TYPE = NSXMLStoreType; |
NSString *STORE_FILENAME = @"CDCLI.cdcli"; |
NSError *error = nil; |
NSURL *url = [applicationLogDirectory() URLByAppendingPathComponent:STORE_FILENAME]; |
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE |
configuration:nil |
URL:url |
options:nil |
error:&error]; |
if (newStore == nil) { |
NSLog(@"Store Configuration Failure\n%@", |
([error localizedDescription] != nil) ? |
[error localizedDescription] : @"Unknown Error"); |
} |
return moc; |
} |
Last updated: 2010-05-24