Reputation: 53
I'm having a problem updating CloudKit records. What I want to achieve is the ability to allow users to vote for a book by clicking a tableview row. This tableview is currently filled using records from Cloud data. I'm stuck when I want to retrieve a record (ie. the selected tableview row), and increase its voteCount column by 1, then save it back to the cloud and reload the data in the tableview.
Currently I am retrieving records by:
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)
}
Can you help me update a particular record and then send it back up to the cloud?
Upvotes: 2
Views: 2355
Reputation: 13127
You would also need code like this:
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
}
}
Upvotes: 2