CloudKit compound query (query with OR)

耗尽温柔 提交于 2019-12-01 17:36:12

问题


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];

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


回答1:


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:

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

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

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



来源:https://stackoverflow.com/questions/30952390/cloudkit-compound-query-query-with-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!