6

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?

Wojtek
  • 1,006
  • 11
  • 30

1 Answers1

8

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

harryhorn
  • 892
  • 6
  • 8
  • I've already tried it. When I use NOT(creatorUserRecordID != %@ AND toUser != %@) I have an error: Terminating app due to uncaught exception 'CKException', reason: 'Expected comparison subpredicate. On the other hand if I try the same without NOT like this: (creatorUserRecordID != %@ AND toUser != %@) it works – Wojtek Jun 21 '15 at 09:08
  • 1
    Maybe there's a reason why they didn't implemented OR in CKRecord? But I can't see why... – Wojtek Jun 21 '15 at 09:09
  • 2
    You are correct. Looking again at the documentation it specifically says: "The NOT compound operator is not supported in the following cases: You cannot use it to negate an AND compound predicate." So it seems you need to query for each separately and then in your code use a SET to find the intersection of both results. Updated my answer to reflect this. – harryhorn Jun 21 '15 at 10:36
  • 5 years later still don't see anything like that. The best what I've done is doing cross-reference to my record and then fetch using equal predicate that returns array of elements in one request – Jakub Jan 02 '20 at 10:49