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

﹥>﹥吖頭↗ 提交于 2019-11-26 21:05:28

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.

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!