Daniel
Daniel

Reputation: 3341

Objective-C : Relationship to own class (CoreData)

In my project there is a managed object called "Group".

This object itself can contain child group objects.

How do I solve this situation in CoreData and in the FetchedResultsController?

My first shot:

http://i46.tinypic.com/zvonpd.png

Thanks, Dan

Upvotes: 1

Views: 467

Answers (2)

gerry3
gerry3

Reputation: 21460

I would make two separate relationships that are inverses of each other.

Group has a to-many relationship with Group named "children", Group also has a to-many relationship with Group named "parents" and they are inverses of each other.

Or, if your data model only calls for one parent: Group has a to-many relationship with Group named "children", Group also has a to-one relationship with Group named "parent" and they are inverses of each other.

Upvotes: 3

TechZen
TechZen

Reputation: 64428

It looks correct. Usually you term the other entities children instead of parents but that is just a matter of style and convention.

You would fetch the child/parent relationship just as you would any other attribute. The only gotcha is that each relationship attribute is returned as a NSSet so you have to find the child you want inside of the set.

Upvotes: 1

Related Questions