带循环的 NSManagedObject 子类的深层复制

发布于 2024-11-14 11:01:21 字数 801 浏览 4 评论 0原文

我希望能够正确地深度复制 NSManagedObject 子类的对象。

我的数据模型的一个高度简化的版本是:“父”对象,每个对象引用(1到多个)多个“子”对象,每个对象引用(多对多)多个“标签”对象。所有对象都是 NSManagedObject 的自定义子类。

问题是,“父”对象还引用(1到多个)其“子”对象使用的所有“标签”对象,加上可能不被任何“子”对象使用的其他“标签”(想法是这为我提供了一种简单的方法来提取潜在“标签”列表,供用户添加到“子项”。)

“子项”或“标签”对象不会被多个“父项”共享。

因此,每个“父级”有多个“子级”和多个“标签”,每个“子级”有一个“父级”和多个“标签”,每个“标签”有一个“父级”和多个“子级”。

我希望能够深度复制“父”对象,维护整个对象树,但我不知道如何解决“父”>“子”>“标签”>“父”的循环... ETC。

如果在复制“父”时,我首先复制所有引用的“子”对象,并且这些“子”对象复制其引用的“标签”并将它们添加到“子”的“父”的“标签”列表中,那么如何做我考虑了那些应该位于“父级”“标签”列表中但未被任何“子级”引用的“标签”。如果我将原始“父级”“标签”列表的副本合并到新列表中,我最终会得到一个列表,其中包含“子”对象中的“标签”(确实想要),以及与某个对象不关联的任何松散“标签” “子”(想要),加上与旧“父”的“子”对象关联的“标签”副本(不想要)。

相反,如果我首先复制“父级”引用的“标签”对象,那么它们就无法链接到它们应该链接到的“子级”对象,因为“子级”对象尚不存在。

啊啊!

如果这听起来有点令人困惑,那是因为事实确实如此。至少,这让我感到困惑。

I want to be able to properly deep copy an object which is a NSManagedObject subclass.

A heavily simplified version of my data model is: 'parent' objects, each of which references (1 to many) several 'child' objects, each of which references (many to many) several 'label' objects. All objects are custom subclasses of NSManagedObject.

The catch is, the 'parent' object also references (1 to many) all the 'label' objects used by its 'child' objects, plus perhaps other 'labels' which may not be used by any 'child' (the idea being that this gives me an easy way to pull up a list of potential 'labels' for a user to add to a 'child'.)

No 'child' or 'label' objects are shared by more than one 'parent'.

So, each 'parent' has multiple 'children' and multiple 'labels', each 'child' has one 'parent' and multiple 'labels', each 'label' has one 'parent' and multiple 'children'.

I want to be able to deep copy 'parent' objects, maintaining the whole object tree, but I don't know how to resolve the loop of 'parent'>'child'>'label'>'parent'…etc.

If, while copying a 'parent', I copy all it's referenced 'child' objects first, and those 'child' objects copy their referenced 'labels' and also added them into that 'child's 'parent's 'labels' list, then how do I account for those 'labels' that should be in the 'parent's 'labels' list that are not referenced by any 'child'. If I union a copy of the original 'parent's 'labels' list into the new one, I end up with a list with 'labels' from the 'child' objects (do want), plus any loose 'labels' not associated with a 'child' (do want), plus copies of the 'labels' that were associated with the old 'parent's 'child' objects (do not want).

Conversely, if I copy the 'parent's referenced 'label' objects first, then they can't be linked to the 'child' objects they should be because the 'child' objects don't exist yet.

Aaaargh!

If this sounds at all confusing, that's because it is. At least, it's confusing me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-11-21 11:01:21

这并不简单,但已在其他问题中得到了一定程度的解决。希望人们能够想出更简单的答案。请参阅:如何在核心数据中制作 NSManagedObject 的深度复制

编辑: 一种策略是使用关系元数据来处理由父/子引起的循环引用。从 NSManagedObject 的实体中,获取 NSRelationshipDescription 对象并检查反向关系。

我使用的第二个策略是树(具有父引用形式的反向链接),其中每个对象只应出现一次。每当克隆对象时,请将对象的原始版本添加到集合中。在从原始结构克隆对象之前,请检查它的设置。如果存在,请勿复制。此外,Core Data 应该为你填写逆关系。如果递归,请在每次方法调用时传递该集合。它对于结构来说需要是全局的。

It is non-trivial but has been addressed somewhat in other questions. Hopefully people can come up with simpler answers though. See: How to make Deep Copy of NSManagedObject in Core Data

EDIT: One strategy is to use relationship metadata for circular references caused by parent/child. From an NSManagedObject's entity, get NSRelationshipDescription objects and check inverse relationships.

A second strategy that I have used is for trees (with back-links in the form of parent references) in which each object should appear only once. Whenever you clone an object, add the original version of the object to a set. Before you clone an object from the original structure, check that set for it. If it exists, do not copy it. In addition, Core Data should fill in the inverse relationship for you. If you recurse, pass the set down with each method call. It needs to be global to the structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文