简体   繁体   English

使用 Core Data、iCloud 和 CloudKit 进行同步和备份以及它们如何协同工作

[英]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.它将是 iOS 8 及更高版本,因此我可以使用 CloudKit。 I did some research and still not clear on how Core Data, iCloud and CloudKit work together.我做了一些研究,但仍然不清楚 Core Data、iCloud 和 CloudKit 如何协同工作。

As far as understand CloudKit is just a way of getting and retrieving data to/from the cloud.据了解,CloudKit 只是一种向/从云端获取和检索数据的方式。 Is CloudKit just a different way of syncing data with iCloud? CloudKit 只是一种与 iCloud 同步数据的不同方式吗?

My questions are:我的问题是:

  1. If I do use CloudKit, do I still need to create local core data database?如果我使用 CloudKit,我还需要创建本地核心数据数据库吗?

    • If yes will it be automatically synced with iCloud or I would have to call methods to store to both places?如果是,它会自动与 iCloud 同步还是我必须调用方法来存储到两个地方?
  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.如果数据仅存储在云中,则用户可以在 iOS 设备未连接到互联网时访问它。 I read that CloudKit will have only limited caching.我读到 CloudKit 只有有限的缓存。

  3. How will that work if iCloud account is not enabled.如果未启用 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 Core Data 用于在本地存储数据

  • iCloud syncs the data and stores in the cloud iCloud 同步数据并存储在云端

  • CloudKit gives the ability to store and manage data in the cloud?? CloudKit 提供了在云中存储和管理数据的能力?

I hope I provided enough info for this question not to get closed.我希望我提供了足够的信息来解决这个问题。

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 本身是完全本地的,不会自动与 Apple 的任何云服务一起使用。
  • Core Data with iCloud enabled turns on syncing via iCloud.启用 iCloud 的Core Data 打开通过 iCloud 同步。 Any changes you save in Core Data are propagated to the cloud, and any changes made in the cloud are automatically downloaded.您保存在 Core Data 中的任何更改都会传播到云,并且会自动下载在云中所做的任何更改。 The data is stored both in iCloud and in a local persistent store file, so it's available even when the device is offline.数据同时存储在 iCloud 和本地持久存储文件中,因此即使设备处于离线状态也可用。 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. CloudKit 与 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.这意味着任何时候你想要读/写云数据,你都需要进行显式的 CloudKit API 调用来做到这一点。 Nothing happens automatically.没有什么会自动发生。 CloudKit does not store data on the device, so the data is not available if the device is offline. CloudKit 不会在设备上存储数据,因此如果设备处于离线状态,数据将不可用。 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. 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.如果您想将 CloudKit 与 Core Data 一起使用,您必须编写自己的自定义代码来在托管对象和 CloudKit 记录之间进行转换。 It's not impossible, but it's more code to write.这并非不可能,但要编写更多代码。 It might be more reliable but it's too soon to say for sure.可能更可靠,但现在下定论还为时过早。

I wrote a blog post describing 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. 2016 年 6 月更新:根据NSPersistentStoreCoordinator最新文档,与 iCloud 核心数据相关的所有内容都被标记为已弃用。 As a result it should probably be avoided for new development.因此,新的开发可能应该避免使用它。

With iOS 13, Apple announced new features in Core Data to better work with CloudKit.在 iOS 13 中,Apple 宣布了 Core Data 中的新功能,以更好地与 CloudKit 配合使用。 The main addition is NSPersistentCloudKitContainer which basically manages syncing between Core Data and CloudKit for you.主要的新增功能是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 镜像核心数据存储

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

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