1
votes

I'm experiencing some wired behavior with core data and iCloud sync. I added the iCloud documents and Cloudkit capabilities to my app and I subscribed to these three notifications:

NSPersistentStoreCoordinatorStoresWillChangeNotification
NSPersistentStoreCoordinatorStoresDidChangeNotification
NSPersistentStoreDidImportUbiquitousContentChangesNotification

The first time I ran the app I received these notifications:

Using local storage: 1
store will Change notification
store did Change notification
Using local storage: 0

but, NSPersistentStoreDidImportUbiquitousContentChangesNotification didn't get fired because core data did not download data from the ubiquitous container of my iCloud account.

However, when I exited the app and relaunched it even with airplane mode turned on, all my data got loaded immediately. It seemed that the data were downloaded and saved to SQLite store on the first launch but the notification wasn't fired so that my UI can be refreshed.

Have anyone experienced such before and what did you do in that situation.

1

1 Answers

0
votes

There's a difference between first-time setup and 'normal' sync operations.

The NSPersistentStoreDidImportUbiquitousContentChangesNotification is not posted during the first-time setup; only when changes later happen. This notifies the app to import them (by calling mergeChangesFromContextDidSaveNotification(_:)).

Apple's iCloud Programming Guide for CoreData is quite helpful for understanding what happens at which stage.

See especially the chapters on iCloud Performs a One-Time Setup and Core Data Posts Content Changes from iCloud.