如何创建一个Core Data谓词来测试一个关系是否包含所有给定的对象? [英] How to create a Core Data predicate to test that a relation contains all given objects?
问题描述
设置:
我有一个与B有一对多关系的核心数据对象A.调用关系items。所以,a.items返回与A相关联的所有B。
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.
现在,我有一个手动组成的B对象的NSSetitemSet。
Now, I have a manually composed NSSet "itemSet" of B objects.
我想要执行以下操作:
return all A objects whose "items" relation exactly matches itemSet
如何构造一个谓词?我试过这个:
How do I construct a predicate for that? I’ve tried this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(ALL items in %@)", itemSet];
但是只是给我不支持的谓词c>。
But that just gives me Unsupported predicate (null)
.
这:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(items in %@)", itemSet];
告诉我谓词
未执行的SQL生成。
tells me unimplemented SQL generation for predicate
. Interesting, but not helpful.
那么,用集合过滤关系的正确方法是什么?
So what’s the right way to filter the relation with a set?
推荐答案
以下谓词可以工作:
[NSPredicate predicateWithFormat:@"(items.@count == %d) AND (SUBQUERY(items, $x, $x IN %@).@count == %d)",
itemSet.count, itemSet, itemSet.count];
谓词首先检查项目数等于给定的 itemSet
,然后检查 itemSet
成员的项目数量是否也等于 itemSet
。如果两者都为真,则项
必须等于 itemSet
。
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
.
这篇关于如何创建一个Core Data谓词来测试一个关系是否包含所有给定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!