How to create a Core Data predicate to test that a relation contains all given objects?

后端 未结 2 1924
一整个雨季
一整个雨季 2020-11-27 22:39

Setup:

I have a Core Data object A that has a to-many relation to B. Call the relation \"items\". So, a.items returns all B-s associated with A.

Now, I have

相关标签:
2条回答
  • 2020-11-27 23:16

    The following predicate could work:

    [NSPredicate predicateWithFormat:@"(items.@count == %d) AND (SUBQUERY(items, $x, $x IN %@).@count == %d)",
                          itemSet.count, itemSet, itemSet.count];
    

    The predicate checks first that the number of items is equal to the size of the given itemSet, and then checks that the number of items which are member of itemSet is also equal to the size of itemSet. If both are true, then items must be equal to itemSet.

    0 讨论(0)
  • 2020-11-27 23:16

    Have you tried:

    NSPredicate *predicate = [NSPredicate predicateWithFormate:@"items == %@", itemSet];
    

    Alternatively, pull out a subset with a simpler predicate and filter them outside of the fetch request. i.e.

    1. Set a predicate for the number of items in the relationship to the be the same as the number of items in your comparison set.
    2. Fetch the results
    3. Filter these results to show only the ones where the sets contain the same items.
    0 讨论(0)
提交回复
热议问题