Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (NSManagedObjectContext *)managedObjectContext
- {
- if (__managedObjectContext != nil) {
- return __managedObjectContext;
- }
- NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
- if (coordinator != nil) {
- NSLog(@"managed object context already set up");
- NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
- [moc performBlockAndWait:^{
- [moc setPersistentStoreCoordinator: coordinator];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
- }];
- __managedObjectContext = moc;
- }
- return __managedObjectContext;
- - (NSManagedObjectModel *)managedObjectModel
- {
- if (__managedObjectModel != nil) {
- return __managedObjectModel;
- }
- NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TheAcademy" withExtension:@"momd"];
- __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
- return __managedObjectModel;
- - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
- {
- if (__persistentStoreCoordinator != nil) {
- return __persistentStoreCoordinator;
- }
- NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TheAcademy.sqlite"];
- __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
- NSPersistentStoreCoordinator* psc = __persistentStoreCoordinator;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // Migrate datamodel.
- NSDictionary *options = nil;
- // This needs to match the entitlements and provisioning profile.
- NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"com.mobileinteraction.theacademy"];
- NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
- if ([coreDataCloudContent length] != 0) {
- // iCloud is available
- cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
- options = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
- [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
- @"TheAcademy.store", NSPersistentStoreUbiquitousContentNameKey,
- cloudURL, NSPersistentStoreUbiquitousContentURLKey,
- nil];
- } else {
- // iCloud is not available
- options = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
- [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
- nil];
- }
- NSError *error = nil;
- [psc lock];
- if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
- {
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- [psc unlock];
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"asynchronously added persistent store!");
- [[NSNotificationCenter defaultCenter] postNotificationName:kRefetchDataBaseObjectsNotification object:self userInfo:nil];
- });
- });
- return __persistentStoreCoordinator;
- - (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc {
- [moc mergeChangesFromContextDidSaveNotification:note];
- NSNotification* refreshNotification = [NSNotification notificationWithName:kRefreshViewsNotification object:self userInfo:[note userInfo]];
- [[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
- - (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
- NSManagedObjectContext* moc = [self managedObjectContext];
- // This only works if you used NSMainQueueConcurrencyType
- // otherwise use a dispatch_async back to the main thread yourself.
- [moc performBlock:^{
- [self mergeiCloudChanges:notification forContext:moc];
- }];
Add Comment
Please, Sign In to add comment