Alex
Alex

Reputation: 378

How to get records with specific IDs?

I can easily get record with specific ID. If I need something specific then I can use CKQueryOperation.

I need to provide it with proper predicate, but I can't specify IDs as parameter of predicate, because it doesn't support CKRecordValue protocol. Another idea is to use CKReference, but how to write proper predicate then?

If I try to use array of Strings and evaluate agains recordID.recordName key path:

    var references:[String] = IDs.map{$0.recordName}
    var predicate = NSPredicate(format: "ANY recordID.recordName IN %@", references)

I get following error:

<CKError 0x17005f3b0: "Invalid Arguments" (12/1009); "Invalid predicate: recordKey (recordID.recordName) contains invalid characters">

Predicate format output:

predicate ANY recordID.recordName IN {"AFF487BE-4A7E-4F76-8185-79FA2E1EFC33", "D4D83B37-97DE-4BF2-9E43-4C5522E36DF1", "0126008A-0B42-4F95-AB31-BB13B6215475", "6DE83AD9-F307-467D-A5EF-AD2B97B8E7F7", "F701ADBF-9BE7-4F42-8CA9-44D2D0E629F8", "38C4FB60-8A65-43F9-8E1E-4C48D5B60DCD"}

Any help is appreciated.

Upvotes: 4

Views: 1450

Answers (1)

Guto Araujo
Guto Araujo

Reputation: 3854

You can use a CKFetchRecordsOperation to fetch records whose RecordIDs you already know:

let fetchRecordsOperation = CKFetchRecordsOperation(recordIDs: recordIDs)
fetchRecordsOperation.desiredKeys = desiredKeys

Init the operation with an array of the CKRecordIDs you'd like to retrieve and it will return each individual record result in the perRecordCompletionBlock and all records fetched in fetchRecordsCompletionBlock.

You can retrieve the complete records by default or just a subset of their values by setting an array of NSStrings to the desiredKeys property with the names of the fields you'd like to retrieve.

Upvotes: 3

Related Questions