开发者

How to define a to-self relationship that's to-many only one way using Core Data?

In a vector graphics manipulation program, I have a "Group" entity which specifies groups of graphics and other groups. A Group can belong to only one other Group, but can contain many other Groups. This is done through a "groups" relationship.

However, in the Xcode data modeling tool, I can't figure out how to do this. I have the groups property but when I select "To-Many Relationship" it's to-many both sides, I want it to be to-many only on one side only. Here is a diagram of how it is 开发者_高级运维at the moment (the "group" relationship highlighted):

How to define a to-self relationship that's to-many only one way using Core Data?

Is there a way to do what I want, or do I need two relationships ("subgroups" and "supergroup") for this?


You need two relationships because each actual instance will have two separate relationships. It will belong to a single "supergroup" but will have many "subgroups". The objects at the other ends of those relationships will not be the same objects.

This is basically the same setup as a tree structure in which each node has a single parent but many children. E.g.

Node{
  parent<<-->Node.children
  children<-->>Node.parent
}

Since each relationship is logically different, they require separate defined relationships in the model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜