谓词不适用于两个 @count 运算符

发布于 2024-12-01 13:24:00 字数 587 浏览 0 评论 0原文

对于具有两个一对多关系(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-12-08 13:24:00

我不知道为什么两个 @count 集合运算符不能在同一个谓词中运行。有时,您会遇到 predicateWithFormat 的奇怪问题。我建议创建两个单独的谓词,然后将它们与以下内容组合:

-[NSCompoundPredicate orPredicateWithSubpredicates];

...直接创建复合谓词而不是依赖解析器通常可以解决此类问题。

但是,您确实希望确保确实拥有两个关系都为空的对象,因为您正在构建的谓词将返回在任一关系中具有一个或多个对象的所有托管对象。

I know of no reason why two @count collection operators couldn't function in the same predicate. Sometime, you hit strange problems with predicateWithFormat. I would suggest creating two separate predicates and then combining them with:

-[NSCompoundPredicate orPredicateWithSubpredicates];

… 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文