简体   繁体   English

查询CloudKit用户记录提供“无法查询系统类型”

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

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. 好的,所以我在CloudKit之上构建游戏,我想查询排名前50位的用户。

// 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">

I found the answer to this one in Apple's docs. 我在Apple的文档中找到了这个答案。 Specifically the docs on CKQueries. 特别是关于CKQueries的文档

In the discussion for initWithRecordType:predicate: is says: 在讨论initWithRecordType:predicate:是说:

You cannot query for user records and executing a query where the record type is set to CKRecordTypeUserRecord results in an error. 您无法查询用户记录并执行查询,其中记录类型设置为CKRecordTypeUserRecord会导致错误。 You must fetch user records directly using their ID. 您必须使用其ID直接获取用户记录。

So I guess CloudKit doesn't let you query User records. 所以我猜CloudKit不允许你查询用户记录。

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. 我查询了然后我可以通过他们的ID获取用户。

With your user CKRecordID you can get the record with 使用您的用户CKRecordID,您可以获得记录

CKDatabase fetchRecordWithID CKDatabase fetchRecordWithID

This way you can read the values like any other CKRecord. 这样您就可以像任何其他CKRecord一样读取值。

Update: 更新:

Take a look to 看一看

CKFetchRecordsOperation fetchCurrentUserRecordOperation() CKFetchRecordsOperation fetchCurrentUserRecordOperation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM