如何使用NSPersistentCloudKitContainer设置有序关系? [英] How to set an ordered relationship with NSPersistentCloudKitContainer?

查看:115
本文介绍了如何使用NSPersistentCloudKitContainer设置有序关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选中与CloudKit一起使用时,出现了错误 Folder.children不能订购.关于有序关系有什么想法吗?

When I checked Used with CloudKit, the error Folder.children must not be ordered appeared. Any idea about an ordered relationship?

IDE:Xcode11 beta3

IDE: Xcode11 beta3

这是文件夹实体.

推荐答案

(适用于:自iOS 13起,以及14的早期测试版)

(applies: as of iOS 13, and early betas of 14)

云工具包不能使用有序的关系(这是很糟糕的,因为有序的数据是存在的基本事物).原因是,所有内容都由CKRecord(云工具包记录)支持,而不是实际的核心数据;这是一种完全不同的数据存储类型,PersistentCloudKitContainer正在将您的数据即时重写到CKRecords并返回.CKRecords没有一种机制可以像我们在Core Data有序关系中一样需要维护有序项目的日记.

Cloud kit can't use ordered relationships (which just blows, as ordered data is a fundamental Thing That Exists). The reason is that everything is backed with CKRecord (cloud kit records) not actual core data stuff -- it's a completely different type of data storage, and the PersistentCloudKitContainer is doing on-the-fly rewrites of your data to CKRecords and back. CKRecords don't have mechanisms that work for maintaining journals of ordered items the way we need in Core Data ordered relationships.

这意味着它不太可能被修复".很快,因为通常需要对CloudKit和苹果的iCloud进行更改(相对于对CoreData进行的更改).

Which means it's not likely to be "fixed" anytime soon, as it would require changes to CloudKit and apple's iCloud generally (vs. just changes to CoreData).

所以...

您有一些不好的选择:

  1. 没有正式的关系-而是有一个存储字段的地方,例如引用列表.添加/删除子对象时,请自己维护.本质上具有管理员"权限.用于维护自己的关系"机制的父记录中的字段".(例如:为每个孩子生成一个UUID,父母为所有孩子存储一个串联的UUID列表)
  2. 使用CoreData Relationships,并将订购数据存储在子对象中,例如您手动维护的orderIndex int字段.
  3. 混合:使用从父母到孩子的无序关系,并且存储孩子的顺序"字段,在父母中.CoreData管理关系,您可以根据需要手动维护和应用这些子项的排序.
  1. Don't have formal Relationships -- instead have a field where you store, e.g., a list of references. Maintain this yourself as you add/remove child objects. Essentially have an "admin" field in parent record that you use to maintain your own Relationship mechanism. (e.g.: generate a UUID for each child, parent stores a concatenated list of UUID's for all children)
  2. Use CoreData Relationships, and store ordering data in the child objects, e.g. orderIndex int field that you maintain manually.
  3. Mixed: use unordered Relationship from parent to children, and also store a field of "order of children" in parent. CoreData manages the relationship, and you can manually maintain and apply ordering of those children as needed.

无论您做什么,这都是不好的:

No matter what you do, it's bad:

  1. 没有关系:没有获取父项";并通过关系获得对孩子的推荐;您必须分别获取两者.也没有删除"消息.为您制定的规则,例如"在删除父级时,级联删除子级.当您需要多个集合中的孩子时,此方法就起作用(因为每个集合记录都拥有自己的孩子列表)

  1. No relationships: no "fetch parent" and get references to children via relationship; you have to fetch both separately. Also no "delete" rules done for you, e.g. " on delete parent, cascade delete children." This works when you need children in more than one collection (as each collection record keeps it's own list of children)

子对象的排序(这是我使用的):您必须在子对象的每个添加和删除操作中插入自己,并运行代码来调整所有子对象的orderIndex值.我通过具有CoreDataManager.createChild(atIndex :),CoreDataManager.deleteChild()和CoreDataManager.moveChild(toIndex :)函数来管理此问题,这些函数具有更新orderIndex值的副作用.但是至少您会获取父级;因为c在parent.children中..."并级联删除的背面.但是:由于子项只有一个orderIndex值,因此子项只能在一个父项的列表中.

Ordering in child objects (this is what I use): you have to insert yourself in every add and delete operation on children, running code to adjust orderIndex values of all children. I manage this by having a CoreDataManager.createChild(atIndex:), CoreDataManager.deleteChild() and CoreDataManager.moveChild(toIndex:) functions that apply side effect of updating orderIndex values. But at least you get "fetch parent; for c in parent.children do..." and cascade delete's back. However: now child can only be in one parent's list, as child only has one orderIndex value.

关系+父级中手动维护的排序字段:让核心数据管理关联,当您需要有序的子级时,可以使用parent.orderedChildren()函数读取父级的.childOrder字段,并将其应用于.children列表.但是...您仍然必须手动管理父级的.childOrder字段,并在每次添加/删除/重新排序子级时对其进行更改.但是,使用Cloud Sync,随着在不同应用程序实例中添加/删除子项时,.child列表和.childOrder字段值不同步的情况可能存在许多潜在的错误.将子列表和排序保持独立意味着可以通过云同步分别更新它们.

Relationship + manually maintained ordering field in parent: let core data manage the association, and when you need ordered children you can use a parent.orderedChildren() function that reads your .childOrder field of parent, applies it to .children list. But... You still have to manage the parent's .childOrder field manually, and change it every time you add/remove/reorder children. HOWEVER, with Cloud Sync, there are many potential bugs wit your .child list and .childOrder field value getting out of sync as children are added/removed in different app instances. Keeping the child list and the ordering separate means they can be updated by cloud sync separately.

这篇关于如何使用NSPersistentCloudKitContainer设置有序关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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