如何高效地在 Core Data 和 CloudKit 之间同步记录 [英] How to sync records between Core Data and CloudKit efficiently

查看:31
本文介绍了如何高效地在 Core Data 和 CloudKit 之间同步记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何使用 CloudKit 框架,但缺少展示如何同步 Core Data 和 CloudKit 的文档或示例.

我已经观看了所有专门用于 CloudKit 的 WWDC 视频(2014、2015、2016),但没有一个视频告诉我们如何实现与 Core Data 的同步.我找不到任何新的示例、教程或书籍来展示如何实现这种同步.

我知道通过 CloudKit(而不是便利 API)使用 Operations API 并订阅更改是有效的,正如它在新的 WWDC 2016 视频中所说的那样,专用于 CloudKit,但与 CoreData 映射是一个真正的问题.

例如,假设我想创建一个类似于 Notes 应用程序的应用程序.离线时,用户可以创建他的笔记并与他们一起工作,将它们保存到他的核心数据数据库中.当设备上线时,应用程序会检查服务器上的更改并将新创建的记录保存到服务器 (CloudKit).

当应用程序启动时,它还会从 CloudKit 获取更改,如果有更改,它会使用新更改更新本地缓存(核心数据).

我希望有一个通用的同步模式.在哪里放置与 Core Data 方法的同步以及它们应该是什么样子?

希望得到任何有关这方面的信息或帮助.

我使用的是 Swift 3、Xcode 8、iOS 10.

解决方案

从 iOS 13 开始,有新的 API 可以为开发人员简化这种同步.我建议您观看有关 CoreData 和 CloudKit 之间新同步的 WWDC19 会议.请注意,这些新 API 仅适用于 iOS 13+.

视频:https://developer.apple.com/videos/play/wwdc2019/202/

简而言之,您需要开始使用 NSPersistentCloudKitContainer 而不是 NSPersistentContainer.这将使同步使用自动冲突解决和最后写入者获胜合并策略自动工作.如果您想构建一个良好的应用程序,您还需要进行一些修改以改进应用程序的同步.

官方文档可以在:

<小时>

用于协作的数据建模(无冲突的复制数据类型)

在会议结束时,他们还展示了一个比默认的last-writer-wins 合并策略"更好的同步合并示例.因果树 的使用允许多个用户编辑相同的字符串(以及某些扩展其他类型的数据)而不会丢失任何数据.我真的建议大家阅读这篇来自 Archagon 的文章 描述了它是如何工作的以及如何实现它(也使用 CloudKit 同步,但不是新的自动同步).正如会议中所演示的,您还可以使用 CoreData 和 CloudKit 之间新的自动同步来实现这一点.

I'm currently learning how to use CloudKit Framework and lack of documentation or examples showing how to sync Core Data and CloudKit.

I have watched all WWDC videos (2014, 2015, 2016) Dedicated to CloudKit, but none of them telling us how to implement syncing with Core Data. I can't find any fresh examples, tutorial or books, showing how to implement this syncing.

I know that it is effective to use Operations API by CloudKit (not Convenience API) and to Subscribe to changes as it said in the new WWDC 2016 videos, dedicated to CloudKit, but mapping with CoreData is a real problem.

For example, let's say I would like to create an app similar to Notes app. while offline, user can create his notes and work with them saving them to his core data database. When the device going online the app checks what changed on the server and saves newly created records to server (CloudKit).

When the app starts, it also fetches for changes from the CloudKit and if there are changes , it updates local cache (Core Data) with the new changes.

I would appreciate to have a common pattern of syncing. Where to place syncing with Core Data methods and how they should look like?

Would appreciate any information or help about this.

I'm using Swift 3, Xcode 8 , iOS 10.

解决方案

As of iOS 13, there are new API's that simplify this synchronization for developers. I would recommend you to watch the WWDC19 session about the new synchronization between CoreData and CloudKit. Please note that these new API's only work for iOS 13+.

Video: https://developer.apple.com/videos/play/wwdc2019/202/

In short, you need to start using NSPersistentCloudKitContainer instead of NSPersistentContainer. This will let the syncing work automatically using automatic conflict resolution with a last-writer-wins merge strategy. If you want to build a good working app, you'll also need to do some modifications to improve the syncing for your app.

Official documentation can be found at:


Data modeling for collaboration (conflict-free replicated data type)

At the end of the session they also demonstrated an example of better sync merging than the default 'last-writer-wins merge strategy'. The usage of Causal Trees allow multiple users to edit the same string (and to some extend other types of data) without losing any data. I would really recommend everyone to read this article from Archagon that describes how this works and how to implement it (also with CloudKit syncing, but not the new automatic one). As demonstrated in the session, you can also implement this with the new automatic syncing between CoreData and CloudKit.

这篇关于如何高效地在 Core Data 和 CloudKit 之间同步记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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