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?

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

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

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • 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 06 '14 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. – James Toomey Mar 20 '18 at 18:43
  • 2
    for me, with Swift 5.1, `let pr = NSPredicate(format: "recordID = %@", CKRecord.ID(recordName: theSearchRecordId))` – wye Dec 27 '19 at 01:28
0

I like:

NSPredicate *predicate= [NSPredicate predicateWithFormat:
      @"recordID = %@",[[CKRecordID alloc] initWithRecordName:theRecordNameForThisRecord]];
Peter B. Kramer
  • 16,385
  • 1
  • 16
  • 20