cloudkitckquery

CKQuery where predicate has reference and not field?


I need to create a CKQuery where the predicate contains a reference of a record, and not a field of the record.

Like this

let query = CKQuery(recordType: "OUP", predicate: NSPredicate(format: "o = %@", "FF4FB4A9-271A-4AF4-B02C-722ABF25BF44")

How do I set o is a CKReference, not field!

I get this error:

Field value type mismatch in query predicate for field 'o'


Solution

  • I found in CKQuery class reference the answer, or at least an example how to use CKReference in CKQuery:

    CKReference* recordToMatch = [[CKReference alloc] initWithRecordID:employeeID action:CKReferenceActionNone];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"employee == %@", recordToMatch];
    

    To match records that link to a different record whose ID you know, create a predicate that matches a field containing a reference object as shown in Listing 1. In the example, the employee field of the record contains a CKReference object that points to another record. When the query executes, a match occurs when the ID in the locally created CKReference object is the same ID found in the specified field of the record.