4

I am sorting the createdAt system field like this:

query.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]

and get the following error:

CKError 0x1c4447fb0: "Invalid Arguments" (12/2018); server message = "Unknown field 'createdAt'"; uuid = 9C450848-2449-4892-93BC-C46363203042; container ID = "...

I am able to query and sort fields that I create in this manner. Anything different with a system field? The field's index is sortable and queryable.

BfromG
  • 55
  • 5

1 Answers1

12

Use creationDate for the key, instead. The meta keys in the CK dashboard vary slightly from the ones used for queries. Here's a list of meta keys:

recordID: CKRecordID
The unique ID of the record.

recordType: String
The app-defined string that identifies the type of the record.

creationDate: Date?
The time when the record was first saved to the server.

creatorUserRecordID: CKRecordID?
The ID of the user who created the record.

modificationDate: Date?
The time when the record was last saved to the server.

lastModifiedUserRecordID: CKRecordID?
The ID of the user who last modified the record.

recordChangeTag: String?
A string containing the server change token for the record.
Brian Hamm
  • 426
  • 3
  • 8
  • That did the trick. Thanks! Any idea why the meta keys are different than on the CK dashboard? – BfromG Apr 23 '18 at 15:45
  • Glad to help. Not sure why the keys themselves are different. But I remember struggling with CloudKit predicates and sort descriptors enough to have created a list of actual keys at some point, and saved them to my Notes app. :) The meta field labels in the /original/ CloudKit dashboard may have matched the keys, but it's been too long for me to remember. – Brian Hamm Apr 25 '18 at 08:22
  • @BrianHamm Why am I not able to access the filed using CKRecord.object(forKey:) when I try to access it using "createdAt" or "creationDate"? How would I access the value of the creationAt field? – daniel Dec 07 '18 at 14:06
  • 2
    @DanielBrower Meta field values should be accessed as a property. Try `myRecord.creationDate` – Brian Hamm Dec 08 '18 at 15:40
  • 2
    Wow this is just absurd..why in the dashboard I see "modifiedAt" and set it as "sortable" and then I have to use "modificationDate"?! BTW thanks you solved the issue. – Matteo Gobbi May 30 '19 at 20:44