The iOS Dev Diary

iCloud in your iOS Apps – Part 4: iCloud and Core Data


Listen Later


In this part we’ll talk about how to make your Core Data powered app work with iCloud. This involves adding an option to your persistent store coordinator which will allow Core Data to write log files to iCloud. When changes are detected, the local Core Data store can merge them using those log files.
Here are the iCloud methods I added to the MasterViewController. First the observers under viewDidLoad:- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;

// add iCloud observers
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(storesWillChange) name:NSPersistentStoreCoordinatorStoresWillChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(storesDidChange:) name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeContent:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:self.managedObjectContext.persistentStoreCoordinator];

}And here are the event handler methods:#pragma mark - iCloud Methods

- (void)storesWillChange {

NSLog(@"\n\nStores WILL change notification received\n\n");

// disbale UI
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];

// save and reset our context
if (self.managedObjectContext.hasChanges) {
[self.managedObjectContext save:nil];
} else {
[self.managedObjectContext reset];
}
}

- (void)storesDidChange:(NSNotification *)notification {

NSLog(@"\n\nStores DID change notification received\n\n");

// enable UI
[[UIApplication sharedApplication]endIgnoringInteractionEvents];

// update UI
[self.tableView reloadData];
}

- (void)mergeContent:(NSNotification *)notification {

NSLog(@"Merge Content here");
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}
Demo Project
Here’s the demo project that I’m building. Don’t forget to change the Bundle ID and your Team ID to make it work on your devices:

* https://github.com/versluis/CoreData-with-iCloud-Screencast

Enjoy creating some rad iCloud apps 😉
Further Reading

* http://pinkstone.co.uk/how-to-test-why-your-icloud-core-data-store-was-changed/
* https://developer.apple.com/LIBRARY/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40013491-CH1-SW1
* http://stackoverflow.com/questions/21977191/icloud-coredata-notification-when-changed-to-icloud-store-first-launch-with

Watch the whole series

* Part 1 is about Setup and Prep Work
* Part 2 is about using Key/Value Storage
* Part 3 is about using Document Storage
* Part 4 is about using Core Data with iCloud
* and Part 5 is about the
...more
View all episodesView all episodes
Download on the App Store

The iOS Dev DiaryBy Jay Versluis

  • 3
  • 3
  • 3
  • 3
  • 3

3

2 ratings