Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data not downloading all data from iCloud

I've been using Core Data in one of my projects for about 6 months now, all working fine, until the other day when a user reported that some of the data from her iOS device wasn't being synced onto her Mac.

When I checked it out it seems that some NSManagedObjects hadn't been imported by the Mac, although they seemed to have been uploaded to iCloud by the iOS device.

I managed to fix the issue by rebuilding the persistent store from iCloud, by passing through the NSPersistentStoreRebuildFromUbiquitousContentOptions with a value of @YES when adding the iCloud enabled persistent store to the persistent store coordinator.

I would like to know if this is a known issue and if it's preventable? Unfortunately I don't have any error reports.

like image 834
Rob Sanders Avatar asked Dec 07 '25 07:12

Rob Sanders


1 Answers

I have the same problem and solved it by removing the persistentStore and adding it again while the app is running. It is always the same problem when doing several changes in the first device, the second device doesn't get all the changes. I use the following code when applicationDidBecomeActive to remove the persistent store first:

var storeCoordinator:NSPersistentStoreCoordinator = persistentStoreCoordinator!
        var store:NSPersistentStore = storeCoordinator.persistentStores[0] as! NSPersistentStore
        var storeURL:NSURL = store.URL!
        storeCoordinator.removePersistentStore(store, error: nil)
        NSFileManager.defaultManager().removeItemAtPath(storeURL.path!, error: nil)

Following I proceed to add the persistentStore again. Then I received all the notifications as it was the first run after installation but with the full data synched. it always works but I don't know if it is the proper way to handle the problem.

I believe that all the changes made in the first device always reach the second device but for some weird reason they don't merge completely with the local core data. I have made some weird observations about the iCloud-CoreData issue in: https://stackoverflow.com/questions/32084952/core-data-to-icloud-doesnt-sync-all-changes][1]

I hope it helps.

like image 78
viktronics Avatar answered Dec 08 '25 21:12

viktronics



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!