我在更新 CloudKit 记录时遇到问题。我想要实现的是允许用户通过单击表格 View 行来投票给一本书。 该表格 View 当前使用云数据中的记录进行填充。 当我想要检索一条记录(即选定的表格 View 行)并将其 voteCount 列增加 1,然后将其保存回云端并重新加载表格 View 中的数据时,我陷入了困境。
目前我正在通过以下方式检索记录:
booksIncludedInVote = []
let cloudContainer = CKContainer.defaultContainer()
let publicDatabase =CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Book", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "author", "image", "description", "voteCount"]
queryOperation.queuePriority = .VeryHigh
queryOperation.resultsLimit = 50
queryOperation.recordFetchedBlock = { (record:CKRecord!) -> Void in
if let restaurantRecord = record {
self.booksIncludedInVote.append(restaurantRecord)
}
您可以帮助我更新特定记录,然后将其发送回云端吗?
最佳答案
您还需要这样的代码:
operation.queryCompletionBlock = { cursor, error in
self.tableView.ReloadData()
}
publicDatabase.addOperation(operation)
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var record = self.booksIncludedInVote[indexPath.row]
// now increment the voteCount value in that record and save it using
publicDatabase.saveRecord(theRecord, completionHandler: { record, error in
}
}
关于ios - SWIFT/CloudKit - 更新记录并将其保存回容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29195258/