在核心数据中维护复杂的单向关系 [英] Maintaining Complex Unidirectional Relationships in Core Data

查看:43
本文介绍了在核心数据中维护复杂的单向关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Realm中的数据模型迁移到Core Data(以便可以与Ensembles同步).该模型是一个图,在Realm中,我自己通过有关事务中删除等的规则来管理图的完整性.

I am migrating a data model I have in Realm to Core Data (so I can sync with Ensembles). The model is a graph, and in Realm I am managing the integrity of the graph myself with rules about deletions etc in transactions.

我在这里制作了一个示例图,旨在描述我的问题的症结所在.

I have made an example graph here that aims to describe the crux of my problem.

图中的所有对象都是物.事物与其子事物之间存在有序的多对多关系.但是,subThings有多种链接方式.例如,一个DerivedFromThing类型具有两个对多关系(它们的逆关系将在另一Thing的subThings中).

All objects in my graph are Things. Things have an ordered to-many relationship with their subThings. However, subThings have multiple ways they link back. For example, one DerivedFromThing type has two to-many relationships (their inverses would be in the subThings of another Thing).

因此,我的图具有完整的逆关系集,但是没有一种似乎可以在Core Data中建模的方式.

So, my graph has a full set of inverse relationships, but not of a fashion that appears to be modelable in Core Data.

由于我已经有了管理完整性的代码,因此我很乐意亲自照顾所有关系.但是,我在构建中收到警告,因此我想再次检查我是否采用了正确的方法.

Since I already have the code to manage integrity, I am happy to look after all the relationships myself. However, I am getting warnings in my build, so I wanted to double-check I am taking the right approach.

有没有一种方法可以在Core Data中为我的许多关系建模?如果没有,我是否会遇到忽略我的警告的问题.

Is there a way to model my many-many relationships in Core Data? If not, are there issues I am going to face ignoring my warnings.

谢谢.

推荐答案

我建议避免CoreData中的单向关系:根据我的经验,当您不说任何逆函数时,CD会将其隐式视为一对一",这就是可能不是您想要的.

I would recommend avoiding unidirectional relationships in CoreData: in my experience when you say nothing about the inverse, CD implicitly treats it as "to-one", which is probably not what you want.

尽管CD可以处理许多关系,但在您的情况下,我认为您可能需要通过直接建模中间实体(或可能两个或更多)来实现关系.例如,您可能有一个名为 Association 且带有

Whilst CD can handle many-many relationships, in your case I think you might need to implement your relationships by directly modelling an intermediate entity (or possibly two or more). For example, you might have an entity named Association, with

  • associationType 的属性(父",超级"或其他)
  • Thing 实体的一对一关系 associatedTo (具有一对多的倒数)
  • DerivedFromThing 实体的一对一关系 associatedFrom (也具有一对多的逆关系)
  • an attribute of associationType ("parent", "super" or whatever)
  • a to-one relationship associatedTo to the Thing entity (with a to-many inverse)
  • a to-one relationship associatedFrom to the DerivedFromThing entity (also with a to-many inverse)

很难提供进一步的建议,因为我怀疑您的真实模型比您的示例更复杂,但是上述方法应该提供高度的灵活性.请参见

It's difficult to advise further, since I suspect your true model is more complicated than your example, but the above approach should provide a good degree of flexibility. See "Modeling a Relationship Based on Its Semantics" in the Core Data Programming Guide for more information.

这篇关于在核心数据中维护复杂的单向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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