ios 使用 Core Data、iCloud 和 CloudKit 进行同步和备份以及它们如何协同工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24441909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Core Data, iCloud and CloudKit for syncing and backup and how it works together
提问by Yan
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.
我正处于创建应用程序的早期阶段,我想在其中保存、同步和备份数据。该应用程序不会将任何文件仅存储在数据库中。它将是 iOS 8 及更高版本,因此我可以使用 CloudKit。我做了一些研究,但仍然不清楚 Core Data、iCloud 和 CloudKit 如何协同工作。
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?
据了解,CloudKit 只是一种向/从云端获取和检索数据的方式。CloudKit 只是一种与 iCloud 同步数据的不同方式吗?
My questions are:
我的问题是:
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?
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.
- How will that work if iCloud account is not enabled.
如果我使用 CloudKit,我还需要创建本地核心数据数据库吗?
- 如果是,它会自动与 iCloud 同步还是我必须调用方法来存储到两个地方?
如果数据仅存储在云中,则用户可以在 iOS 设备未连接到互联网时访问它。我读到 CloudKit 只有有限的缓存。
- 如果未启用 iCloud 帐户,这将如何工作。
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??
Core Data 用于在本地存储数据
iCloud 同步数据并存储在云端
CloudKit 提供了在云中存储和管理数据的能力?
I hope I provided enough info for this question not to get closed.
我希望我提供了足够的信息来解决这个问题。
回答by Tom Harrington
It's like this:
就像这样:
- Core Data on its own, is completely local and does not automatically work with any of Apple's cloud services.
- Core Data with iCloud enabledturns 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 is not related to Core Data. It's not a sync system, it's a transfer system. Meaning that any time you want to read/write cloud data, you need to make explicit CloudKit API calls to do so. Nothing happens automatically. CloudKit does not store data on the device, so the data is not available if the device is offline. CloudKit also adds some features not available to Core Data with iCloud-- like public shared data and the ability to download only part of the data set instead of the whole thing.
- Core Data 本身是完全本地的,不会自动与 Apple 的任何云服务一起使用。
- 启用 iCloud 的Core Data打开通过 iCloud 同步。您保存在 Core Data 中的任何更改都会传播到云,并且会自动下载在云中所做的任何更改。数据同时存储在 iCloud 和本地持久存储文件中,因此即使设备处于离线状态也可用。您不必编写任何特定于云的代码,您只需要添加对传入更改的监听(这很像在不同的托管对象上下文中所做的更改)。
- CloudKit 与 Core Data 无关。这不是同步系统,而是传输系统。这意味着任何时候您想要读取/写入云数据,您都需要进行显式 CloudKit API 调用来执行此操作。没有什么会自动发生。CloudKit 不会在设备上存储数据,因此如果设备处于离线状态,数据将不可用。CloudKit 还添加了一些 iCloud 无法用于 Core Data 的功能——例如公共共享数据以及仅下载部分数据集而不是整个数据集的能力。
If you wanted to use CloudKit with Core Data, you'd have to write your own custom code to translate between managed objects and CloudKit records. It's not impossible, but it's more code to write. It mightbe more reliable but it's too soon to say for sure.
如果您想将 CloudKit 与 Core Data 一起使用,您必须编写自己的自定义代码来在托管对象和 CloudKit 记录之间进行转换。这并非不可能,但要编写更多代码。它可能更可靠,但现在下定论还为时过早。
I wrote a blog postdescribing CloudKit from the perspective of someone who's used Core Data and iCloud in the past.
我写了一篇博文,从过去使用 Core Data 和 iCloud 的人的角度描述 CloudKit。
Update, June 2016: As of the most recent documentation for NSPersistentStoreCoordinator
, everything related to Core Data with iCloud is marked as deprecated. As a result it should probably be avoided for new development.
2016 年 6 月更新:根据 的最新文档NSPersistentStoreCoordinator
,与 iCloud 核心数据相关的所有内容都被标记为已弃用。因此,新的开发可能应该避免使用它。
回答by Pomme2Poule
With iOS 13, Apple announced new features in Core Data to better work with CloudKit. The main addition is NSPersistentCloudKitContainerwhich basically manages syncing between Core Data and CloudKit for you.
在 iOS 13 中,Apple 宣布了 Core Data 中的新功能,以更好地与 CloudKit 配合使用。主要的新增功能是NSPersistentCloudKitContainer,它基本上为您管理 Core Data 和 CloudKit 之间的同步。
You can learn more in the WWDC session Using Core Data with CloudKit.
您可以在 WWDC 会议将 Core Data 与 CloudKit 结合使用来了解更多信息。
Apple also released a nice collection of docs for this very usage: Mirroring a Core Data store with CloudKit.
Apple 还针对这种用法发布了一系列不错的文档:使用 CloudKit 镜像核心数据存储。