ak3nat0n
ak3nat0n

Reputation: 6298

Core data many to many parent child relationship

I apologize if this question exists already on the site posted a different way but after browsing I couldn't find anything. I have an entity in core data called category which stores categories and subcategory objects. I would like to have create a relationship between categories and sub-categories. In my app, I have the following requirements:

So really, sub-categories are just categories.

What I would like is to find a way to create my category entity to have that parent child relationship as well as a list of parents and a list of children for each category.

Can someone suggest an approach for this problem? would I need a "junction" entity? What would a fetch request look like to get a category object from the entity and populate its parents and children lists while taking advantage of the whole object graph concept (if possible)

Thank you.

Upvotes: 1

Views: 573

Answers (1)

Mundi
Mundi

Reputation: 80271

Simply create the Category entity and add two relationships to itself: subCategories and parentCategories, which are of course each others reverse relationships. Now you can access the corresponding lists very simply:

NSSet *children = category.subCategories;
NSSet *parents  = category.parentCategories; 

Upvotes: 2

Related Questions