9

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
Rob van den Berg
  • 800
  • 5
  • 21

2 Answers2

8

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

Rob van den Berg
  • 800
  • 5
  • 21
  • 1
    Thanks, strange CKRecordID works for single comparisons – malhal Dec 10 '15 at 00:08
  • 1
    @SamBallantyne Just convert your record ID's to CKReferences like let refs = myRecords.map({ CKReference(recordID: $0.record.recordID, action: .None) }) – DogCoffee Mar 26 '16 at 06:35
1

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)