Database Reference
In-Depth Information
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *docURL = nil;
docURL = [[fileManager URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = nil;
NSError *error = nil;
NSPersistentStoreCoordinator *coordinator = nil;
coordinator = [[self managedObjectContext] persistentStoreCoordinator];
NSPersistentStore *store = nil;
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
We start the changes at the top of the asynchronous dispatch queue. Notice
that we are setting the “universal” options only for the NSPersistentStore at this
point. This step allows us to reuse the dictionary no matter what path we end
up taking. We are also obtaining our reference to the NSPersistentStoreCoordinator
here, because that will be used through the rest of the block. Finally, we
request the cloudURL from the NSFileManager so that we can start to determine
how to add the NSPersistentStore to the NSPersistentStoreCoordinator .
Now we are ready to make our first decision: is iCloud available or not?
iCloud/PPRecipes/PPRAppDelegate.m
if (!cloudURL) {
storeURL = [docURL URLByAppendingPathComponent:@ "PPRecipes.sqlite" ];
store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&error];
if (!store) {
ALog(@ "Error adding persistent store to coordinator %@\n%@" ,
[error localizedDescription], [error userInfo]);
//Present a user facing error
return ;
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self contextInitialized];
});
}
Now that we've added the migration code for iCloud, it is actually the shorter
path when iCloud is not enabled. Therefore, we are going to respond to that
decision first. If iCloud is not available, we look for the file named PPRecipes.sqlite
and add it to the persistent store. If the file does not exist, Core Data will
create it. This is the traditional logic path.
 
 
 
Search WWH ::




Custom Search