Core Data iCloud Syncing

2020-07-22 09:48发布

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条回答
啃猪蹄的小仙女
2楼-- · 2020-07-22 10:25

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.

查看更多
登录 后发表回答