【问题标题】:Querying CloudKit Users Record gives "Can't query system types"查询 CloudKit 用户记录给出“无法查询系统类型”
【发布时间】:2014-12-16 17:05:27
【问题描述】:

好的,所以我正在 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)

这不会将任何记录添加到记录数组中,它会在完成块中返回此错误:

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

【问题讨论】:

    标签: ios objective-c swift cloudkit


    【解决方案1】:

    我在 Apple 的文档中找到了这个问题的答案。特别是CKQueries.上的文档

    initWithRecordType:predicate: 的讨论中说:

    您无法查询用户记录,并且执行记录类型设置为 CKRecordTypeUserRecord 的查询会导致错误。您必须直接使用他们的 ID 获取用户记录。

    所以我猜 CloudKit 不允许你查询用户记录。

    我最终添加了一条包含用户参考的分数记录。我对此进行了查询,然后我可以通过他们的 ID 获取用户。

    【讨论】:

    • 令人震惊,这使得在用户记录中存储其他字段完全没用!一旦看到它以复数形式打破常规,就应该知道更好,这将是一个令人讨厌的惊喜。
    • 我找到了一个不错的解决方法。我创建了我自己的 User 表,其中包含我需要能够查询的所有字段,并且我包含一个 userRecordName 字段,该字段存储 CloudKit 的 recordName 对应于默认 Users 表中存储的内容。
    【解决方案2】:

    使用您的用户 CKRecordID,您可以获取记录

    CKDatabase fetchRecordWithID

    通过这种方式,您可以像读取任何其他 CKRecord 一样读取值。

    更新:

    去看看

    CKFetchRecordsOperation fetchCurrentUserRecordOperation()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2011-03-18
      相关资源
      最近更新 更多