SWIFT / CloudKit - 更新记录并将其保存回容器

时间:2015-03-22 13:58:48

标签: ios swift cloudkit

我在更新CloudKit记录时遇到问题。我想要实现的是通过单击tableview行允许用户投票的能力。 此表视图当前使用来自云数据的记录填充。 当我想要检索记录(即选定的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)
    }

您能帮助我更新特定记录,然后将其重新发送到云端吗?

1 个答案:

答案 0 :(得分:2)

您还需要这样的代码:

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