I\'m attempting to sync Core Data among devices using iCloud (using the new iOS7 method). I\'m still seeing issues with the syncing when testing with the simulators. Sometimes d
Here is how I do it. It might be an easy way to simplify and unclutter your code.
self.managedObjectContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSMainQueueConcurrencyType];
self.managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
self.managedObjectContext.persistentStoreCoordinator =
[[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:self.managedObjectModel];
[self.managedObjectContext.persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeURL
options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore" }
error:&error];
The only iCloud bit is the options parameter. (I removed other options for clarity.) The store URL is just [documentsDirectory URLByAppendingPathComponent:@"Store.sqlite"]
.
I am also listening to these:
NSPersistentStoreDidImportUbiquitousContentChangesNotification
NSPersistentStoreCoordinatorStoresWillChangeNotification
NSPersistentStoreCoordinatorStoresDidChangeNotification
In the first one, I call
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:note];
and post another notification to update the UI; in the second one, I save; in the third one I also update the UI.
For me that's really all there is to it. It works flawlessly up to now.
Credit goes to iCloudCoreDataStack.