2

I am trying to use a database managed by Core Data and synced with iCloud in my iPhone app.

I have used the sample code from the WWDC 2012 and managed to make it work almost properly.

Also, my database is very simple: there is only one kind of object and no relationship.

Two devices can communicate through iCloud, however, this little scenario fails:

  • Create an object on the device A
  • Wait that the object appeared on the device B
  • Modify the object on the device A
  • Modify the object on the device B
  • When the device B is aware of the update it tries to merge the changes and the app crashes.

Yes, the app CRASHES. There is only ONE object in my database and Core Data doesn't seem to be able to resolve a tiny little conflict.

Here is how I create my persistent store and with which options:

NSDictionary *options = @{NSPersistentStoreUbiquitousContentNameKey : contentName,
NSPersistentStoreUbiquitousContentURLKey : iCloudLogsPath};
self.iCloudStore = [self.persistentStoreCoordinator  
    addPersistentStoreWithType:NSSQLiteStoreType
    configuration:nil
    URL:iCloudData
    options:options
    error:&error];

And here is the code I use to create a NSManagedObjectContext in background:

- (NSManagedObjectContext*) currentThreadContext {

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [moc setPersistentStoreCoordinator:self.persistentStoreCoordinator];
    [moc setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
    return moc;
}

I did add a breakpoint to catch any exceptions, here is what I get:

objc_exception_throw:
[NSPersistentStoreCoordinator executeRequest:withContext:error:]:
[NSManagedObjectContext save:]:
[_PFUbiquityRecordImportOperation main]:
[__NSOperationInternal start]:
[_PFUbiquityRecordsImporter scheduleTransactionLogOperations:synchronous:error:]:
[_PFUbiquityRecordsImporter scanOperationFinished:withDiscoveredLogLocation:error:]:
[PFUbiquityImportScanOperation main]:
[__NSOperationInternal start]:
[_PFUbiquityRecordsImporter scheduleUbiquityRootScan:withLocalPeerLogs:error:]:
[_PFUbiquityRecordsImporter rollResponseOperation:successfullyAdoptedBaseline:]:
[PFUbiquityBaselineRollResponseOperation main]:
[__NSOperationInternal start]:

Any help would be highly appreciated!

Thomas Castel
  • 553
  • 1
  • 6
  • 11
  • 1
    Core Data iCloud syncing is a big bag of hurt. Be prepared for many more crashes. A couple of resources before your long journey: https://github.com/blackpixel/NSConferenceiPhoneCoreDataRecipes and http://mentalfaculty.tumblr.com/post/23163747823/under-the-sheets-with-icloud-and-core-data-the-basics – nevan king Jan 30 '13 at 20:10
  • Do you ever get the (deep breath) `NSPersistentStoreDidImportUbiquitousContentChangesNotification`? Apple's WWDC 2012 sample code rather bizarrely leaves this out, and listening for and handling it is necessary. – Tom Harrington Jan 30 '13 at 20:15
  • Yes I receive it. If I don't modify an object from 2 devices at the same time, everything works fine. Of course in the real life simultaneous changes will occur all the time... – Thomas Castel Jan 30 '13 at 20:27
  • Unfortunately, weird inexplicable problems are just business as usual when using Core Data's built in iCloud support. – Tom Harrington Jan 30 '13 at 23:52
  • Yes perhaps but this is a really dumb one, I can't believe it... It must probably be something I didn't set up properly or something. – Thomas Castel Jan 31 '13 at 07:41
  • Without wanting to dishearten you (I myself am still hopeful), take a look at my current [question](http://stackoverflow.com/questions/14478517/more-icloud-core-data-synching-woes). – Mundi Jan 31 '13 at 09:56
  • @ThomasCastel were you able to fix this issue,me too facing same issue due primary key of a value in core data,please check this question too http://stackoverflow.com/questions/36545965/using-core-data-icloud-and-issue-with-live-sync – Infaz Apr 12 '16 at 06:37

0 Answers0