Search code examples
core-datajoinleft-joinnsfetchrequestright-join

Core Data. Join 2 tables with many-tomany relationship?


I have 2 tables which are linked between themselves with many-to-many relationship.

SQL equivalent doesn't exist because it requires the third table to split a many-to-many relationship to two one-to-many relationships.

For example, I have two core data entities: category with a property (an array of items) and item with a property (an array of categories).

I need to get all the categories except of "empty" categories (when there is no item of this category).

My current temporary solution looks like an incorrect one. I use NSFetchRequest to get all the categories. Then I delete from this array all the categories with an empty item array manually using for-each.


Solution

  • My solution:

    [NSPredicate predicateWithFormat:@"ANY items != nil"]