开发者

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

开发者 https://www.devze.com 2023-03-25 10:55 出处:网络
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.

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

精彩评论

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