带有 NONE 的 NSPredicate 聚合操作 [英] NSPredicate Aggregate Operations with NONE
问题描述
如何创建可以获取的谓词:所有问题不包含 answer.correct = "1"
.
How do I create a predicate that can fetch: all questions does not contain answer.correct = "1"
.
如果返回的数组包含0"和1",则以下谓词不起作用:
The following predicate doesn't work if the returned array contain "0" and "1":
[NSPredicate predicateWithFormat:@"NONE answers.correct IN %@", [NSArray arrayWithObject:@"1"]];
也尝试过 NOT (ANY ...) :相同的结果
Also tried with NOT (ANY ...) : same result
这是一个错误吗?
推荐答案
简短回答: 要获取所有没有任何正确 == 1"答案"的对象,请使用以下命令子查询:
Short answer: To get all objects that do not have any "answer" with "correct == 1", use the following SUBQUERY:
[NSPredicate predicateWithFormat:@"SUBQUERY(answers, $a, $a.correct == 1).@count == 0"]
解释:两个谓词
[NSPredicate predicateWithFormat:@"NONE answers.correct == 1"]
[NSPredicate predicateWithFormat:@"NOT (ANY answers.correct == 1)"]
应该有效(据我了解 NOT 和 ANY 关键字),但它们无效.它们返回与
should work (as I understand the NOT and ANY keywords) but they don't. They return the same result set as
[NSPredicate predicateWithFormat:@"ANY answers.correct != 1"]
通过设置启动参数 -com.apple.CoreData.SQLDebug 3
并检查 SQL 选择语句可以看出.
as can be seen by setting the launch argument -com.apple.CoreData.SQLDebug 3
and inspecting the SQL select statements.
这对我来说似乎是一个 Core Data 错误.使用 SUBQUERY 可以解决该问题.
This seems like a Core Data bug to me. Using the SUBQUERY is a workaround for that problem.
这篇关于带有 NONE 的 NSPredicate 聚合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!