Querying CloudKit Users Record gives “Can't query system types”

眉间皱痕 提交于 2019-12-08 21:23:32

问题


Okay, so I'm building a game on top of CloudKit and I want to query the users with the top 50 scores for a leaderboard.

// Create a CKQuery
let predicate = NSPredicate(value: true)
let sortDescriptor = NSSortDescriptor(key: "score", ascending: false)
var query = CKQuery(recordType: "Users", predicate: predicate)
query.sortDescriptors = [sortDescriptor]

// Create a query operation
var queryOperation = CKQueryOperation(query: query)
queryOperation.resultsLimit = 50
queryOperation.recordFetchedBlock = { (record: CKRecord!) -> Void in
    self.records.append(record)
}
queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor!, error: NSError!) in
    // Log an error and show and alert
    if error != nil {
        println("Error querying for leaderboard: \(error)")
        var alert = UIAlertController(title: "Unable Donwload Leaderboard", message: "Unable to download the leaderboard at this time. Please try agin later.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
        return
    }

    // Update the records array and refresh the table view
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
}

// Start the operation
database.addOperation(queryOperation)

This does not add any records to the records array and it comes back with this error in the completion block:

<CKError 0x1553cc10: "Permission Failure" (10/2007); server message = "Can't query system types"; uuid = 3349514B-02EC-40D7-B716-585D4ADD3128; container ID = "iCloud.com.MyCompany.MyApp">

回答1:


I found the answer to this one in Apple's docs. Specifically the docs on CKQueries.

In the discussion for initWithRecordType:predicate: is says:

You cannot query for user records and executing a query where the record type is set to CKRecordTypeUserRecord results in an error. You must fetch user records directly using their ID.

So I guess CloudKit doesn't let you query User records.

I ended up adding a score record with a reference to the users. I queried that and then I could get the users by their ID.




回答2:


With your user CKRecordID you can get the record with

CKDatabase fetchRecordWithID

This way you can read the values like any other CKRecord.

Update:

Take a look to

CKFetchRecordsOperation fetchCurrentUserRecordOperation()



来源:https://stackoverflow.com/questions/27510024/querying-cloudkit-users-record-gives-cant-query-system-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!