简体   繁体   中英

Core Data iCloud Syncing

I have implemented Core Data as local storage for a new app and now want to enable iCloud synchronising across devices. I've been reading the documentation and some tutorials but can't get my head around how to switch between local and cloud storage, and how to synchronise changes from iCloud when cloud storage is enabled. Would somebody mind summarising the process so I can then go and research each stage? Thanks in advance!

I've managed to get this working by following this tutorial:

http://corsarus.com/2015/using-core-data-in-ios-part-4-core-data-syncing-with-icloud/

To summarise the changes I needed to make to my existing Core Data app:

  • Change the store URL to a different name which does not conflict with the local store's URL
  • Include the cloudOptions dictionary when adding the persistent store
  • Register for notifications of NSPersistentStoreCoordinatorStoresWillChangeNotification, NSPersistentStoreCoordinatorStoresDidChangeNotification and NSPersistentStoreDidImportUbiquitousContentChangesNotification
  • Implement methods called by observing those notifications which save and reset the context, update the UI and merge incoming changes from iCloud respectively

Swapping between local and cloud storage will be less straightforward but I'm confident that I understand enough now to give this a try.

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