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? {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
deregisterForStoreChanges()
registerObservers(persistentStoreCoordinator: coordinator)
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeSettings.url, options: storeSettings.options)
} catch {
abort()
}
persistentStoreCoordinator = coordinator
let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
managedObjectContext.mergePolicy = NSMergePolicy(mergeType:NSMergePolicyType.MergeByPropertyStoreTrumpMergePolicyType )
return managedObjectContext
}
And my NSPersistentStoreCoordinatorStoresDidChangeNotificationfunc function
func persistentStoreCoordinatorDidChangeStores(notification:NSNotification){
if notification.userInfo?[NSAddedPersistentStoresKey] != nil {
print("-----------------------")
print("ADDED")
print("-----------------------")
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, , , . , . , , , , .