CloudKit复合查询(使用OR查询) [英] CloudKit compound query (query with OR)

查看:145
本文介绍了CloudKit复合查询(使用OR查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有两个字段的OR来​​查询CloudKit。但我找不到办法如何做到这一点。我做的是:

I would like to query CloudKit using OR with two fields. 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];

但遗憾的是CKQuery不支持OR查询(如文档中所述)我能否实现其他一些效果方式?

But unfortunately CKQuery does not support OR query (as written in documentation) Can I achieve this effect some other way?

推荐答案

CKQuery支持AND和NOT,所以你可以想象你可以使用简单的布尔代数来创建一个基于事实上,NOT(不是A而不是B)== A或B.但是,文档明确说明:

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:

在以下情况下不支持NOT复合运算符:

"The NOT compound operator is not supported in the following cases:

你不能用它来否定AND复合谓词。

You cannot use it to negate an AND compound predicate."

所以你必须分别查询每个ORed谓词,将它们作为SET保存,然后取两个集合的交集来得到最终结果

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

这篇关于CloudKit复合查询(使用OR查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆