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

后端 未结 2 1950
一整个雨季
一整个雨季 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.

提交回复
热议问题