1
votes

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:

  • sub-categories can belong to multiple categories.
  • Categories can have multiple sub-categories
  • A category will hold a list of its parents and children.

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.

1

1 Answers

2
votes

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;