swiftcloudkitckrecordckquery

Can I sort CKRecord's when queried by a user entered Date/Time field?


I am saving multiple fields in different records in CloudKit. When I query the records I currently have them sorted by creation date. Am I able to sort these records by the Date/Time fields I have my users save to the record and not by the creation or modification date of the records?

@objc func queryDatabaseColby() {
    let query = CKQuery(recordType: "ColbyFlight", predicate: NSPredicate(value: true))
    database.perform(query, inZoneWith: nil) { (records, _) in
        guard let records = records else {return}
        let sortedRecords = records.sorted(by: { $0.creationDate! > $1.creationDate! })
        self.colbyFlight = sortedRecords
        DispatchQueue.main.async {
        }
    }
}

Solution

  • Replaced sorted by creation date with sorted by: value(forKey: "date") and got it to do what I want.