gpt4 book ai didi

ios - 为什么将 Core Data 与 CloudKit 一起使用时关系必须是可选的?

转载 作者:行者123 更新时间:2023-12-03 20:43:21 27 4
gpt4 key购买 nike

以下是 Apple 的 doc 中将 Core Data 与 Cloudkit 一起使用的要求之一。 :

All relationships must be optional. Due to operation size limitations,relationship changes may not be saved atomically.


尝试使用与 CloudKit 的可选关系会导致错误:

Thread 1: Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=CloudKit integration requires that all relationships be optional, the following are not:Some_Managed_Object: some_attribute}, ["NSLocalizedFailureReason": CloudKit integration requires that all relationships be optional, the following are not:Some_Managed_Object: some_attribute]


我想知道,这不是完全违背了使用关系的目的吗?
例如,假设我有两个实体: AccountTransfer .由于转账始终与源账户和目标账户相关联, Transfer应该有两个与 Account 的非可选关系.但是由于上述要求,这些关系必须是可选的。
该文档给出了解释:“(这是因为)关系更改可能无法自动保存”。这似乎表明,在 Cloudkit 和 Core Data 之间同步期间,关系可能不完整 并且不完整的关系暴露给App代码 .这对我来说似乎是一个严重的问题,因为:
  • 在我上面的例子中,这两个关系本质上是非可选的。将它们更改为可选会使模态变得毫无意义。
  • 即使在那些关系应该是可选的示例中,虽然不完整的关系在语法上是正确的,但它可能会导致意外的不一致问题。

  • 所以我想知道这应该如何在真正的应用程序中工作?这对我来说似乎很 splinter 。我是不是误会了什么? 难道是使用 Cloudkit 来同步 Core Data 只适用于一小部分只使用可选关系的应用程序吗? (如果是这样,我想知道其他 Core Data 应用程序如何在设备之间同步它们的数据。)

    相关说明:像许多其他人一样,我努力搜索有关 Cloudkit 和 Core Data 使用的同步和冲突解决算法的详细信息。我能找到的仅有的几条信息是:
  • https://developer.apple.com/forums/thread/121196

  • In an eventually consistent distributed system you can never "know"that you have existing data or devices in the cloud. Your applicationwill simply "find out at some point" that this data exists and needsto be designed to handle that


  • https://mjtsai.com/blog/2019/06/04/syncing-core-data-with-cloudkit-and-nspersistentcloudkitcontainer/

  • Yup, Core Data CloudKit implements to-many relationships using CRDTs!


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

  • Conflict resolution is implemented automatically byNSPersistentCloudKitContainer using a last writer wins merge policy.


    虽然我大致了解这些信息中的每一条,但它们并没有直接得出以下结论:1) Cloudkit 和 Core Data 之间的数据更改是否以原子方式同步?更重要的是 2) 在同步期间是否将不完整的数据暴露给 App 代码?
    我的猜测是 1) 否和 2) 是的。但是,如果在同步期间不完整的数据更改暴露给 App 代码,我很难理解如何编写真正的应用程序。 是不是,要使用 Cloudkit 来同步 Core Data,必须将模式设计为​​在不完整的关系下正常工作?
    如果有人可以分享您的理解方式,我将不胜感激。

    最佳答案

    Could it be that, to use Cloudkit to sync Core Data, the modal has to be designed to work fine with incomplete relationship?


    基本上就是这样——模型和与模型一起工作的代码需要满足这个标准。
    当 CloudKit 从区域交付更改的记录时,不能保证操作在单个“交付”中包含完整的对象图(参见: recordZoneFetchResultBlock),因此核心数据团队决定部分数据集的优先级高于原子数据集(如前所述)。我不能代表他们说话,但我对这个方向的假设是由于性能和复杂性的原因。
    以一个新客户端或一段时间未连接的设备为例,需要消耗 1,000 条记录:该数据的传递可能分为 2 次行程(获取结果 block 调用),第一次包含 700 条记录(与它自己的部分转移更改 token )和第二个带有最后 300 个(以及最新的存储更改 token )。 CloudKit 不 promise 在任何一次旅行中完成或有序交付完成图表所需的内容(在某些情况下,甚至可能无法在一次旅行中发送完整图表),这将导致在增量期间无法满足所需的关系保存( see this answer)。否则,Core Data 需要在将任何内容提交到磁盘之前,从内存中的云存储中搅动每条记录,以正确保持完整性。
    不幸的是,这意味着您的代码需要在访问/处理它之前通过确保它是有效的来处理关系。如果您需要保证关系客户端,因为没有其他方法可以解耦对象图功能,您可能需要深入 CloudKit 框架并构建查询操作以确认 CloudKit 数据集中的关系或获取操作来处理导入以原子方式获取数据,而不是依赖于自动行为。

    关于ios - 为什么将 Core Data 与 CloudKit 一起使用时关系必须是可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66380409/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com