使用 Core Data、iCloud 和 CloudKit 进行同步和备份以及它们如何协同工作 [英] Using Core Data, iCloud and CloudKit for syncing and backup and how it works together

查看:58
本文介绍了使用 Core Data、iCloud 和 CloudKit 进行同步和备份以及它们如何协同工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于创建应用程序的早期阶段,我想在其中保存、同步和备份数据.该应用程序不会将任何文件仅存储在数据库中.它将是 iOS 8 及更高版本,因此我可以使用 CloudKit.我做了一些研究,但仍然不清楚 Core Data、iCloud 和 CloudKit 如何协同工作.

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.

据了解,CloudKit 只是一种向/从云端获取和检索数据的方式.CloudKit 只是一种与 iCloud 同步数据的不同方式吗?

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?

我的问题是:

  1. 如果我使用 CloudKit,我还需要创建本地核心数据数据库吗?

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

  • 如果是,它会自动与 iCloud 同步还是我必须调用方法来存储到两个地方?

如果数据仅存储在云中,则用户可以在 iOS 设备未连接到互联网时访问它.我读到 CloudKit 只有有限的缓存.

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.

如果有人可以分解每种技术在离线和在线保存和同步核心数据数据库的过程中的作用.

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

我目前的理解是:

  • Core Data 用于本地存储数据

  • Core Data is used to store data locally

iCloud 同步数据并存储在云端

iCloud syncs the data and stores in the cloud

CloudKit 能够在云中存储和管理数据??

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.

推荐答案

是这样的:

  • Core Data 本身是完全本地的,不会自动与 Apple 的任何云服务配合使用.
  • Core Data 启用 iCloud 开启通过 iCloud 同步.您保存在 Core Data 中的任何更改都会传播到云中,并且会自动下载在云中所做的任何更改.数据同时存储在 iCloud 和本地持久存储文件中,因此即使设备处于离线状态也可用.您不必编写任何特定于云的代码,只需添加对传入更改的监听(这很像在不同的托管对象上下文中所做的更改).
  • CloudKit 与 Core Data 无关.这不是同步系统,而是传输系统.这意味着任何时候您想要读取/写入云数据,您都需要进行显式的 CloudKit API 调用来执行此操作.没有什么会自动发生.CloudKit 不会在设备上存储数据,因此如果设备离线,数据将不可用.CloudKit 还添加了一些使用 iCloud 的 Core Data 不可用的功能,例如公共共享数据以及仅下载部分数据集而不是整个数据集的能力.
  • Core Data on its own, is completely local and does not automatically work with any of Apple's cloud services.
  • Core Data with iCloud enabled 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 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.

如果您想将 CloudKit 与 Core Data 一起使用,您必须编写自己的自定义代码来在托管对象和 CloudKit 记录之间进行转换.这并非不可能,但要编写更多代码.它可能更可靠,但现在下定论还为时过早.

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 might be more reliable but it's too soon to say for sure.

我写了一篇博文,从某人的角度描述了 CloudKit谁曾经使用过 Core Data 和 iCloud.

I wrote a blog post describing CloudKit from the perspective of someone who's used Core Data and iCloud in the past.

更新,2016 年 6 月:截至 最新文档code>NSPersistentStoreCoordinator,所有与 iCloud 核心数据相关的内容都被标记为已弃用.因此,在新的开发中应该避免使用它.

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.

这篇关于使用 Core Data、iCloud 和 CloudKit 进行同步和备份以及它们如何协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆