11

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?

2 Answers 2

23

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
    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. Mar 20, 2018 at 18:43
  • 2
    for me, with Swift 5.1, let pr = NSPredicate(format: "recordID = %@", CKRecord.ID(recordName: theSearchRecordId))
    – wye
    Dec 27, 2019 at 1:28
0

I like:

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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