Padin215
Padin215

Reputation: 7484

Core Data reflexive to-many relationships

Need to set up an Entity Person that has a to-many relationship to itself (reflexive).

So each Person can have n sub person, basicly this:

enter image description here

Does Person have two Relationships, person and sub_person? How does the inverse rule work?

Upvotes: 2

Views: 718

Answers (3)

robhasacamera
robhasacamera

Reputation: 3326

It is definitely possible to have a object in Core Data related to itself in this way. In the Table, Graph editor for the data model, hold down the control key and draw a line out from Person and back to itself. This will give you a line with an arrow on each end, both pointing at Person. Then rename the relationships (parentPerson, subPersons) and make subPersons a to-many relationship in the property inspector. The end result should look like this:

enter image description here

This style of data management follows a design pattern known as the Composite Pattern.

Upvotes: 1

Martin R
Martin R

Reputation: 539915

If I understand your requirement correctly, you have to define only one entity Person, and two relationships:

  • sub_persons as to-many relationship from Person to Person,
  • super_person as to-one relationship from Person to Person,

and define these as inverse relationships of each other.

Upvotes: 2

Caleb
Caleb

Reputation: 125007

An entity can certainly be related to itself, in the same way that a node in a doubly linked list has next and previous relationships with other nodes. Remember that an entity is like a class -- it's a kind of object rather than a particular object. So, for example, your Person entity might have a friends relation to Person -- a given person may have many friends, all of whom are also instances of Person.

Upvotes: 2

Related Questions