CloudKit.js: Sort by Created Date

Hi,


I've got my CloudKit records with created and modifed setup to be sortable and queryable but I'm having a hard time constructing a CloudKit.js query that will use those values.


This:


  var query = {
    recordType: "MyRecord",
    sortBy: [{
      fieldName: 'created'
    }]
  };


This doesn't work, I get this error:


Unknown field 'CKFieldName{_name='created'}


I also tried 'createdDate', 'modified', 'modificationDate' and all give the same error. What's the magic?

Same here. I've tried the same things but I'm always getting a 'field name error'.

did you try 'creationDate'?


https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKRecord_class/#//apple_ref/occ/instp/CKRecord/creationDate


https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKQuery_class/

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. You cannot use key paths to specify fields in related records.

Unknown field 'CKFieldName{_name='creationDate'}

Same for createDate, created, creationTime, createTime which i've all seen mentioned in similar questions.

Maybe this simply does not work via the REST API.

S.

Let me be the first to say this is probably not safe to use. I could not find any documentation on it and figured it out by brute force. I imagine Apple will replace this with a proper and documented method at some point in the future... but for now I've been able to sort by creation and modification dates by using:


var querySortedByCreationDate = {
     recordType: 'RECORD_TYPE',
     sortBy: [{
          fieldName: '___createTime',
          ascending: true
     }]
}

var querySortedByModificationDate = {
     recordType: 'RECORD_TYPE',
     sortBy: [{
          fieldName: '___modTime',
          ascending: true
     }]
}

Did not notice there are three underscores, but this really helped me.


Thanks

From:

https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/CloudKitWebServicesReference/QueryingRecords.html#//apple_ref/doc/uid/TP40015240-CH5-SW4

Queries can be configured against system fields as well as developer-defined fields. The system fields are special fields on which an index can be defined. These fields have a special mapping such that the ___ prefix does not need to be used.

The sort descriptor dictionary can take a key systemFieldName instead of fieldName where the mapped values can be used.

You also need to add a 'Sortable' Index to the Record's CreatedTimestamp Field in CloudKit Console.

CloudKit.js: Sort by Created Date
 
 
Q