13

CKQuery doc says: Key names used in predicates correspond to fields in the currently evaluated record. Key names may include the names of the record’s metadata properties such as "creationDate” or any data fields you added to the record.

What else metadata can I use in a CKQuery? Am I able to use record.recordID.recordName, if yes, what is the key for it?

3 Answers 3

24

Yes, you could create a CKQuery for searching a recordID like this:

var query = CKQuery(recordType: recordType, predicate: NSPredicate(format: "%K == %@", "creatorUserRecordID" ,CKReference(recordID: theSearchRecordId, action: CKReferenceAction.None)))

Where theSearchRecordId is the recordID.recordName that you are looking for

Metadata fields are recordID, recordType, creationDate, creatorUserRecordID, modificationDate, lastModifiedUserRecordID, recordChangeTag

See https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKRecord_class/index.html#//apple_ref/doc/uid/TP40014044-CH1-SW14

3
  • 15
    holy macaroni it works, I was looking for this more then a months, expression for my case: let pr = NSPredicate(format: "recordID = %@", CKRecordID(recordName: theSearchRecordId))
    – János
    Commented Oct 6, 2014 at 20:18
  • I had to add a Queryable index on the createdBy field in the CloudKit dashboard before it would let me query (it kept giving me an .invalidArguments error), but after that it worked. Commented Mar 20, 2018 at 18:43
  • 2
    for me, with Swift 5.1, let pr = NSPredicate(format: "recordID = %@", CKRecord.ID(recordName: theSearchRecordId))
    – wye
    Commented Dec 27, 2019 at 1:28
0

I like:

NSPredicate *predicate= [NSPredicate predicateWithFormat:
      @"recordID = %@",[[CKRecordID alloc] initWithRecordName:theRecordNameForThisRecord]];
0

While all the replies have managed to answer the question correctly, here is a bit more modern approach to building a CKQuery predicate would be to use the #keypath() expression.

let recordType: String // Record Type of the record you are looking for
let id: CKRecord.ID // ID of the record you are looking for

let predicate = NSPredicate(
   format: "%K == %@", #keyPath(CKRecord.recordID), id
)
let query = CKQuery(
    recordType: recordType, predicate: predicate
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.