Search code examples
iosswifticloudcloudkit

CloudKit: Order query results by creation date


When loading items from CloudKit I would like to order the results by creation date. I believe now it's sorting by the first property of the record.

func loadItems() {
    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "Items", predicate: predicate)
    db.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
        if error != nil {
            println(error.localizedDescription)
        }

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            items = results as [CKRecord]
            self.tableView.reloadData()
            println("items: \(items)")
        }
    }
}

Solution

  • This is what I was looking for:

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