Core Data + iCloud Sync has changed the settings of stores, does not update the user interface

I can not deal with this problem, I am trying to integrate iCloud and Core Data into my application, and I am stuck with the iCloud synchronization part.

My full scenario:

  • Local master data store seeded with source data
  • Later, the application will ask the user about iCloud or local stoge data
  • If the user selects iCloud, the current local storage is migrated to iCloud storage.
  • After migration, the contextual and persistent storage coordinator restarts using the iCloud Store
  • Refresh data from a new context (problem here)

If we cancel the discussion of migration and focus on loading the permanent storage coordinator with iCloud, I think the problem is related to the NSPersistentStoreCoordinatorStoresDidChangeNotificationevent.

I just don't get it. In every article I read, I saw something like: "reload the UI here." But this does not work for me.

Update Coordinator Function

func configureContext() -> NSManagedObjectContext? {
    // Create the coordinator and context
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

    //remove all observers from "previous" coordinator
    deregisterForStoreChanges()
    //and register them for new one
    registerObservers(persistentStoreCoordinator: coordinator)

    do {

        //storeSettings.url = applicationDocumentsDirectory.URLByAppendingPathComponent("iCloud.sqlite")
        //storeSettings.options = [NSPersistentStoreUbiquitousContentNameKey:"iCloudProject"]

        try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeSettings.url, options: storeSettings.options)

    } catch {
        //Here was standard error handler I didn't changed it, but removed for listing
        abort()
    }
    persistentStoreCoordinator = coordinator

    let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator

    //not quite sure about the next string, didn't test it yet
    managedObjectContext.mergePolicy = NSMergePolicy(mergeType:NSMergePolicyType.MergeByPropertyStoreTrumpMergePolicyType )

    //tried to refetch my records here but, with no luck.
    //refetchUIRecords()
    return managedObjectContext
}

And my NSPersistentStoreCoordinatorStoresDidChangeNotificationfunc function

func persistentStoreCoordinatorDidChangeStores(notification:NSNotification){

    if notification.userInfo?[NSAddedPersistentStoresKey] != nil {
        print("-----------------------")
        print("ADDED")
        print("-----------------------")
        //deduplicateRecords()

        //if iCloud -> refetch UI
        if NSUserDefaults.standardUserDefaults().boolForKey("iCloud"){
            self.createTimer()
        }
    }
    print("Did Change")

}

Creating a timer function is just a function that waits 1 second. before actually retyping and updating the user interface.

Problem

When we reach step 4 from my script and call the configureContext function, I see this in the console:

2016-04-12 13:31:27.749 TestingiCloudProject[2052:1142902] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](898): CoreData: Ubiquity:  mobile~567404C0-9D84-4C07-A0F8-D25832CB65D8:iCloudProject
Using local storage: 1 for new NSFileManager current token <ef7a917f bca47ecf 5d58862d cbe9998d 7e53e5ea>
Did Change
Did Change
-----------------------
ADDED
-----------------------
Timer
Did Change
Refetch
Refetching...
Number of records after fetch: 1 //must be more than 1 
2016-04-12 13:31:30.090 TestingiCloudProject[2052:1143055] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](898): CoreData: Ubiquity:  mobile~567404C0-9D84-4C07-A0F8-D25832CB65D8:iCloudProject
Using local storage: 0 for new NSFileManager current token <ef7a917f bca47ecf 5d58862d cbe9998d 7e53e5ea>

As you can see, my fetch request is executed before Using local storage: 0and why I take records from local storage (1 record), and not iCloud (which contains more than 1).

, . , Core Data iCloud. , 1 , 2 , , , iCloud- , , , - .

, - .

Apple dev SO, , , . , . , , , , .

+4
1

NSPersistentStoreDidImportUbiquitousContentChangesNotification, , . NSManagedObjectContext.

ubiquity iCloud, Core Data NSPersistentStoreDidImportUbiquitousContentChangesNotification. userInfo , NSManagedObjectContextDidSaveNotification, , NSManagedObjectID, NSManagedObject. , , . mergeChangesFromContextDidSaveNotification: , , Core Data.

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html

0

All Articles