简体   繁体   中英

Core data + iCloud Sync NSPersistentStoreDidImportUbiquitousContentChangesNotification

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM