Database Reference
In-Depth Information
Asynchronously Adding the NSPersistentStore
Prior to iOS 6.0 and Mac OS X Lion, we could add the NSPersistentStore to the
NSPersistentStoreCoordinator directly on the main thread. While this was rarely
recommended, it was extremely common. With the addition of iCloud, it's
really no longer an option. The process of configuring iCloud happens when
we add the NSPersistentStore to the NSPersistentStoreCoordinator , and it happens before
the call returns. If iCloud needs to download data and that download takes
several seconds, our application will be unresponsive while the download
occurs, and our application could be potentially killed from the watchdog for
taking too long to start up.
Currently, the best solution to this problem is to add the NSPersistentStore to
the NSPersistentStoreCoordinator on a background thread. We can use dispatch
queues and blocks to make this relatively painless.
iCloud/PPRecipes/PPRAppDelegate.m
dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSInferMappingModelAutomaticallyOption];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
if (cloudURL) {
DLog(@ "iCloud enabled: %@" , cloudURL);
cloudURL = [cloudURL URLByAppendingPathComponent:@ "PPRecipes" ];
[options setValue:[[NSBundle mainBundle] bundleIdentifier]
forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setValue:cloudURL
forKey:NSPersistentStoreUbiquitousContentURLKey];
} else {
DLog(@ "iCloud is not enabled" );
}
NSURL *storeURL = nil;
storeURL = [[fileManager URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
storeURL = [storeURL URLByAppendingPathComponent:@ "PPRecipes-iCloud.sqlite" ];
NSError *error = nil;
NSPersistentStoreCoordinator *coordinator = nil;
coordinator = [[self managedObjectContext] persistentStoreCoordinator];
NSPersistentStore *store = nil;
store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
 
 
 
Search WWH ::




Custom Search