János
János

Reputation: 35038

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'

Upvotes: 8

Views: 4026

Answers (2)

Senseful
Senseful

Reputation: 91641

You can use a CKRecord or CKRecordID, with or without a CKReference, to match relationships.

CKRecord:

let predicate = NSPredicate(format: "artist == %@", artist)

CKRecordID:

let predicate = NSPredicate(format: "artist == %@", artistID)

CKReference with CKRecord:

let recordToMatch = CKReference(record: artist, action: CKReferenceAction.None)
let predicate = NSPredicate(format: "artist == %@", recordToMatch)

CKReference with CKRecordID:

let recordToMatch = CKReference(recordID: artistID, action: CKReferenceAction.None)
let predicate = NSPredicate(format: "artist == %@", recordToMatch)

Upvotes: 15

János
János

Reputation: 35038

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.

Upvotes: 11

Related Questions