SWIFT/CloudKit - 更新记录并将其保存回容器
SWIFT/CloudKit - Updating a record and saving it back to container
我在更新 CloudKit 记录时遇到问题。我想要实现的是允许用户通过单击表格视图行来为一本书投票的能力。
此表视图当前使用云数据中的记录填充。
当我想检索记录(即选定的 tableview 行)并将其 voteCount 列增加 1,然后将其保存回云端并重新加载 tableview 中的数据时,我被卡住了。
目前我正在通过以下方式检索记录:
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
}
}
我在更新 CloudKit 记录时遇到问题。我想要实现的是允许用户通过单击表格视图行来为一本书投票的能力。 此表视图当前使用云数据中的记录填充。 当我想检索记录(即选定的 tableview 行)并将其 voteCount 列增加 1,然后将其保存回云端并重新加载 tableview 中的数据时,我被卡住了。
目前我正在通过以下方式检索记录:
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
}
}