ioscloudkitckmodifyrecordsoperation

The order in which iCloud records are modified


I'm now using CKModifyRecordsOperation to add 5 records to iCloud. My code is like this:

let recordsToSave: [CKRecord] = [record1, record2, record3, record4, record5]
let operation = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: [])

After successfully modifying the 5 records, I use CKQueryOperation to query all records, my code is like this:

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: RECORDTYPE, predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "modificationDate", ascending: true)]
let operation = CKQueryOperation(query: query)

I thought the order of the fetched records would be record1-record2-record3-record4-record5.

But, actually, the order is chaotic. It might be 1-3-5-4-2, or it might be 2-4-5-1-3, or any other. It depends on iCloud itself. But I checked these 5 records in iCloud Dashboard, these 5 records's modifation date are the same.

Is there any way to achieve the order I want?


Solution

  • When databases are sorting on a particular property, they will generally return records that have the same value for that property in an arbitrary order, for performance reasons. If you need to have that order preserved for those records, you could do one of two things:

    Not knowing more specific details about your app, I would lean towards the secondary ID solution. If you don't want to make your database more complicated, and your app wouldn't be modify huge batches of records at one time, the latter solution may be more appropriate.