简体   繁体   English

CloudKit复合查询(使用OR查询)

[英]CloudKit compound query (query with OR)

I would like to query CloudKit using OR with two fields. 我想使用带有两个字段的OR来​​查询CloudKit。 But I can't find a way how to do this. 但我找不到办法如何做到这一点。 What I did is: 我做的是:

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"(creatorUserRecordID == %@)", userId];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(toUser == %@)", userId];
NSCompoundPredicate *compPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate1, predicate2]];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Message" predicate:compPredicate];

But unfortunately CKQuery does not support OR query (as written in documentation) Can I achieve this effect some other way? 但遗憾的是CKQuery不支持OR查询(如文档中所述)我可以通过其他方式实现此效果吗?

CKQuery supports AND and NOT, so you would imagine you could use simple boolean algebra to create a query based on the fact that NOT(NOT A AND NOT B) == A OR B. HOWEVER, the documentation specifically says: CKQuery支持AND和NOT,所以你可以想象你可以使用简单的布尔代数来创建一个基于NOT(NOT A AND NOT B)== A OR B这一事实的查询。但是,文档具体说:

"The NOT compound operator is not supported in the following cases: “在以下情况下不支持NOT复合运算符:

You cannot use it to negate an AND compound predicate." 你不能用它来否定AND复合谓词。“

So you must query for each of the ORed predicates separately save them each as a SET and then take the intersection of the two sets to get the final result 因此,您必须分别查询每个ORed谓词,将它们作为SET保存,然后取两个集合的交集以获得最终结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM