在 watchOS 2.0 中使用核心数据

Using Core Data with watchOS 2.0

我用 WatchKit 1.0 制作了一个 iOS/Apple Watch 应用程序,它使用共享组容器和自定义框架来访问相同的核心数据存储。现在,在 watchOS 2.0 的过渡指南中,Apple 是这样说的:

If your existing Watch app and iOS app use a shared group container or iCloud to exchange data, you must change the way you exchange that data in watchOS 2. Because the WatchKit extension now runs on Apple Watch, the extension must exchange data with the iOS app wirelessly. You can do that using an NSURLSession object or using the Watch Connectivity framework, which supports bidirectional communication between your iOS app and WatchKit extension.

但是,这如何与 Core Data 一起工作?我是否需要维护两个单独的存储,一个在手表上的文档文件夹中,一个在 phone 上,并使用 Watch Connectivity 框架来同步更改?我看到 watchOS 2.0 可以访问 Core Data 框架(它在 Available Technologies 的列表中)......但我不确定当商店位于 phone 时这有什么帮助。任何 help/guidance 不胜感激!

是的,您必须维护两个独立的商店。如果任何一方都是 "read-only" 客户端并且 CoreData 数据存储很小并且不经常更改,您可以潜在地使用 transferFile WatchConnectivity API 在每次更改时传输整个存储。

我建议在第一次 运行.

上传输存储后,使用用户信息后台传输设置同步以双向(如果需要)镜像每个数据库操作

WWDC Session 713 - Introducing Watch Connectivity 涵盖了所需的机制。很好 API 他们放在一起。