I'm having some weird issues with iCloud and Core Data syncing across devices. The problem is that records are syncing sporadically and certain notifications are blank.
For example, if I add three records to the database, only two of the three records will sync. Same thing for deletions. However, when I rebuild the data store from scratch (NSPersistentStoreRebuildFromUbiquitousContentOption
), all the correct entries are present. As you can see in the log file below, I think the issue is that the Notifications are not populating correctly. The middle change should be a deletion but nothing is populated.
See below a copy of the logs and the code I am using for mergeChangesFromContextDidSaveNotification
.
Logs:
- 2015-05-02 00:05:42.339 Tracker[1370:219748] persistentStoreDidImportUbiquitousContentChanges
- 2015-05-02 00:05:42.339 Tracker[1370:219748] NSConcreteNotification 0x17485dbe0 {name = com.apple.coredata.ubiquity.importer.didfinishimport; object = ; userInfo = { deleted = "{(\n 0xd00000002fe40000 \n)}"; inserted = "{(\n)}"; updated = "{(\n)}"; }}
- 2015-05-02 00:05:42.341 Tracker[1370:219748] Underlying data changed ... refreshing!
- 2015-05-02 00:05:44.293 Tracker[1370:219748] persistentStoreDidImportUbiquitousContentChanges
- 2015-05-02 00:05:44.293 Tracker[1370:219748] NSConcreteNotification 0x170a498a0 {name = com.apple.coredata.ubiquity.importer.didfinishimport; object = ; userInfo = { deleted = "{(\n)}"; [SHOULD BE A DELETION BUT NOTHING] inserted = "{(\n)}"; updated = "{(\n)}"; }}
- 2015-05-02 00:05:44.308 Tracker[1370:219748] Underlying data changed ... refreshing!
- 2015-05-02 00:05:46.266 Tracker[1370:219748] persistentStoreDidImportUbiquitousContentChanges
- 2015-05-02 00:05:46.266 Tracker[1370:219748] NSConcreteNotification 0x174a5c0b0 {name = com.apple.coredata.ubiquity.importer.didfinishimport; object = ; userInfo = { deleted = "{(\n 0xd00000002fe00000 \n)}"; inserted = "{(\n)}"; updated = "{(\n)}"; }}
- 2015-05-02 00:05:46.292 Tracker[1370:219748] Underlying data changed ... refreshing!
Code:
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserverForName:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:self.persistentStoreCoordinator
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(@"persistentStoreDidImportUbiquitousContentChanges");
NSLog(@"%@", note);
[self.managedObjectContext performBlock:^{
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:note];
NSNotification* refreshNotification =
[NSNotification notificationWithName:@"iCloudReloadTable"
object:self
userInfo:[note userInfo]];
[[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
}];
}];