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

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

问题描述

我目前正在学习如何使用CloudKit Framework,并且缺少文档或示例来展示如何同步Core Data和CloudKit.

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

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

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.

我知道,使用CloudKit的Operations API(而非便利性API)并订阅专用于CloudKit的新WWDC 2016视频中所说的更改是有效的,但是使用CoreData进行映射是一个真正的问题.

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.

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

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).

应用启动时,它还会从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.

我希望有一种通用的同步模式.与Core Data方法进行同步的位置以及它们的外观如何?

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.

我正在使用Swift 3,Xcode 8和iOS 10.

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

推荐答案

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

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+.

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

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

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:

  • Setting Up Core Data with CloudKit
  • Syncing a Core Data Store with CloudKit

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

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

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天全站免登陆