Using Core Data, iCloud and CloudKit for syncing and backup and how it works together

I am in the early stages of creating an app where I would like to save, sync and backup data. The app will not store any files just data in a database. It is going to be iOS 8 and up so I am able to use CloudKit. I did some research and still not clear on how Core Data, iCloud and CloudKit work together.

As far as understand CloudKit is just a way of getting and retrieving data to/from the cloud. Is CloudKit just a different way of syncing data with iCloud?

My questions are:

  1. If I do use CloudKit, do I still need to create local core data database?

    • If yes will it be automatically synced with iCloud or I would have to call methods to store to both places?
  2. If the data is only stored in the cloud will user be able to access it when iOS device is not connected to the internet. I read that CloudKit will have only limited caching.

  3. How will that work if iCloud account is not enabled.

If someone can kind of break down what each technology does in the process of saving and syncing core data database offline and online.

My current understanding is:

  • Core Data is used to store data locally

  • iCloud syncs the data and stores in the cloud

  • CloudKit gives the ability to store and manage data in the cloud??

I hope I provided enough info for this question not to get closed.

31146 次浏览

是这样的:

  • Core Data on its own, is completely local and does not automatically work with any of Apple's cloud services.
  • Core Data 启用 iCloud turns on syncing via iCloud. Any changes you save in Core Data are propagated to the cloud, and any changes made in the cloud are automatically downloaded. The data is stored both in iCloud and in a local persistent store file, so it's available even when the device is offline. You don't have to write any cloud-specific code, you just need to add listening for incoming changes (which is a lot like changes made on a different managed object context).
  • CloudKit 与核心数据无关。这不是同步系统,这是传输系统。这意味着无论何时您想读/写云数据,都需要进行显式的 CloudKit API 调用。没有什么是自动发生的。CloudKit 不在设备上存储数据,因此如果设备脱机,则数据不可用。CloudKit 还增加了一些 iCloud 中核心数据不具备的特性——比如公共共享数据,以及只能下载部分数据集而不是整个数据集的能力。

如果希望将 CloudKit 与核心数据一起使用,则必须编写自己的自定义代码来在托管对象和 CloudKit 记录之间进行转换。这不是不可能,但是需要编写更多的代码。它的 也许吧更可靠,但现在下结论还为时过早。

我从一个曾经使用过 Core Data 和 iCloud 的人的角度写了 一篇博客文章来描述 CloudKit。

更新 ,2016年6月: 从 NSPersistentStoreCoordinator的最新文档开始,所有与 iCloud 核心数据相关的内容都被标记为已弃用。因此,对于新的开发,可能应该避免这种情况。

随着 iOS13的推出,苹果公司宣布了核心数据的新功能,以便更好地与 CloudKit 协同工作。主要的补充是 云集容器,它主要为您管理 Core Data 和 CloudKit 之间的同步。

You can learn more in the WWDC session Using Core Data with CloudKit.

苹果公司还发布了一个很好的文档集,就是用于这个用途的: 使用 CloudKit 镜像核心数据存储