Core Data iCloud Syncing

£可爱£侵袭症+ 提交于 2019-12-06 07:53:40

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/36388308/core-data-icloud-syncing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!