谓词不适用于两个 @count 运算符
对于具有两个一对多关系(collectionA 和 collectionB)的实体,此代码将谓词分配给 NSFetchRequest:
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(collectionA.@count > 0 ) OR (collectionB.@count > 0)" ]];
但该谓词不起作用,并且所有对象都被选中。如果我为另一行更改该行:
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"collectionA.@count > 0" ]];
那么它会按预期工作,并且返回在 collectionA 中至少包含一项的所有对象。集合B 也是如此。但是当我尝试使用 OR 运算来获取两者的组合时,它不会过滤任何对象,就好像谓词被忽略一样。
我确信我做错了什么,也许两个 @count 运算符不能在同一个谓词中使用。我必须如何编写谓词才能获取集合 A 或集合 B 中任何项目的对象的结果?
提前致谢。
This code assigns a predicate to a NSFetchRequest, for a entity that has two one to many relationships (collectionA and collectionB):
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(collectionA.@count > 0 ) OR (collectionB.@count > 0)" ]];
But that predicate doesn't work and all of the objects are selected. If I change that line for this other one:
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"collectionA.@count > 0" ]];
Then it works as expected and it returns all the objects with at least one item in collectionA. The same for collectionB. But as I try the OR operation to get the combination of both, then it doesn't filter any object as if the predicate was ignored.
I'm sure I am doing something wrong and perhaps two @count operators cannot be used in the same predicate. How do I have to write a predicate to get the results of objects with any item in collectionA or in collectionB?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么两个
@count
集合运算符不能在同一个谓词中运行。有时,您会遇到predicateWithFormat
的奇怪问题。我建议创建两个单独的谓词,然后将它们与以下内容组合:...直接创建复合谓词而不是依赖解析器通常可以解决此类问题。
但是,您确实希望确保确实拥有两个关系都为空的对象,因为您正在构建的谓词将返回在任一关系中具有一个或多个对象的所有托管对象。
I know of no reason why two
@count
collection operators couldn't function in the same predicate. Sometime, you hit strange problems withpredicateWithFormat
. I would suggest creating two separate predicates and then combining them with:… creating the compound predicate directly instead of relying on the parser often resolves this type of problem.
However, you do want to make sure that you do in fact have objects that have both relationships empty because the predicate you are building will return all managed objects that have one or more objects in either relationship.