简体   繁体   English

核心数据iCloud同步

[英]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. 我已将Core Data实施为新应用程序的本地存储,现在希望启用跨设备的iCloud同步。 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. 我一直在阅读文档和一些教程,但无法了解如何在本地和云存储之间切换,以及如何在启用云存储时同步iCloud中的更改。 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/ 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: 总结我需要对现有Core Data应用程序进行的更改:

  • Change the store URL to a different name which does not conflict with the local store's URL 将商店URL更改为与本地商店的URL不冲突的其他名称
  • Include the cloudOptions dictionary when adding the persistent store 添加持久性存储时包括cloudOptions字典
  • Register for notifications of NSPersistentStoreCoordinatorStoresWillChangeNotification, NSPersistentStoreCoordinatorStoresDidChangeNotification and NSPersistentStoreDidImportUbiquitousContentChangesNotification 注册NSPersistentStoreCoordinatorStoresWillChangeNotification,NSPersistentStoreCoordinatorStoresDidChangeNotification和NSPersistentStoreDidImportUbiquitousContentChangesNotification的通知
  • Implement methods called by observing those notifications which save and reset the context, update the UI and merge incoming changes from iCloud respectively 通过观察保存和重置上下文的通知来实现调用的方法,更新UI并分别合并来自iCloud的传入更改

Swapping between local and cloud storage will be less straightforward but I'm confident that I understand enough now to give this a try. 在本地和云存储之间进行交换将不那么简单,但我相信我现在已经足够了解这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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