Guest User

Untitled

a guest
Jan 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. - (NSManagedObjectContext *)managedObjectContext
  2. {
  3. if (__managedObjectContext != nil) {
  4. return __managedObjectContext;
  5. }
  6.  
  7. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  8. if (coordinator != nil) {
  9. NSLog(@"managed object context already set up");
  10. NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  11.  
  12. [moc performBlockAndWait:^{
  13. [moc setPersistentStoreCoordinator: coordinator];
  14.  
  15. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
  16. }];
  17. __managedObjectContext = moc;
  18. }
  19. return __managedObjectContext;
  20.  
  21. - (NSManagedObjectModel *)managedObjectModel
  22. {
  23. if (__managedObjectModel != nil) {
  24. return __managedObjectModel;
  25. }
  26. NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TheAcademy" withExtension:@"momd"];
  27. __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  28. return __managedObjectModel;
  29.  
  30. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  31. {
  32. if (__persistentStoreCoordinator != nil) {
  33. return __persistentStoreCoordinator;
  34. }
  35.  
  36. NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TheAcademy.sqlite"];
  37. __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  38.  
  39. NSPersistentStoreCoordinator* psc = __persistentStoreCoordinator;
  40. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  41. NSFileManager *fileManager = [NSFileManager defaultManager];
  42.  
  43. // Migrate datamodel.
  44. NSDictionary *options = nil;
  45.  
  46. // This needs to match the entitlements and provisioning profile.
  47. NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"com.mobileinteraction.theacademy"];
  48. NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
  49. if ([coreDataCloudContent length] != 0) {
  50. // iCloud is available
  51. cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
  52.  
  53. options = [NSDictionary dictionaryWithObjectsAndKeys:
  54. [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  55. [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
  56. @"TheAcademy.store", NSPersistentStoreUbiquitousContentNameKey,
  57. cloudURL, NSPersistentStoreUbiquitousContentURLKey,
  58. nil];
  59. } else {
  60. // iCloud is not available
  61. options = [NSDictionary dictionaryWithObjectsAndKeys:
  62. [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  63. [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
  64. nil];
  65. }
  66.  
  67. NSError *error = nil;
  68. [psc lock];
  69. if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
  70. {
  71. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  72. abort();
  73. }
  74. [psc unlock];
  75.  
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. NSLog(@"asynchronously added persistent store!");
  78. [[NSNotificationCenter defaultCenter] postNotificationName:kRefetchDataBaseObjectsNotification object:self userInfo:nil];
  79. });
  80.  
  81. });
  82. return __persistentStoreCoordinator;
  83.  
  84. - (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc {
  85. [moc mergeChangesFromContextDidSaveNotification:note];
  86.  
  87. NSNotification* refreshNotification = [NSNotification notificationWithName:kRefreshViewsNotification object:self userInfo:[note userInfo]];
  88.  
  89. [[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
  90.  
  91. - (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
  92. NSManagedObjectContext* moc = [self managedObjectContext];
  93.  
  94. // This only works if you used NSMainQueueConcurrencyType
  95. // otherwise use a dispatch_async back to the main thread yourself.
  96. [moc performBlock:^{
  97. [self mergeiCloudChanges:notification forContext:moc];
  98. }];
Add Comment
Please, Sign In to add comment