Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query CloudKit for recordID IN [CKRecordID]

My predicate wants to exclude some records that are already downloaded and available in a [CKRecordID]. Now I can query 1 CKRecordID[0], but not the [CKRecordID] array. How can I query the array?

let excludeIDs: [CKRecordID]

This works:

let pred1 = NSPredicate(format: "NOT(recordID = %@)", excludeIDs[0])

But this doesn't:

let pred1 = NSPredicate(format: "NOT(recordID IN %@)", excludeIDs)

ERROR: loadImageCompareRecordIDsAndEndDateThatHaveNotEnded Error: Invalid predicate: Invalid predicate: Array members must conform to CKRecordValue: ( "", "", "", "", "" ) (CKRecordID)

The other general parts of the code:

    let sort = NSSortDescriptor(key: "creationDate", ascending: false)
    let query = CKQuery(recordType: MyRecordTypes.ImageCompare, predicate: pred1)
    query.sortDescriptors = [sort]
    let operation = CKQueryOperation(query: query)
    operation.desiredKeys = ["endDate"]
    operation.resultsLimit = 50
like image 536
Rob van den Berg Avatar asked Oct 02 '15 03:10

Rob van den Berg


2 Answers

Using [CKReference] and not [CKRecordID] solved it.

like image 147
Rob van den Berg Avatar answered Oct 15 '22 14:10

Rob van den Berg


To be explicit (because it took me hours to get this right)...

let refs = excludeIDs.map { CKRecord.Reference(recordID: $0.recordID, action: .none) }
let pred1 = NSPredicate(format: "NOT(recordID IN %@)", refs)
like image 1
Dave Thompson Avatar answered Oct 15 '22 13:10

Dave Thompson