阅读背景:

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

来源:互联网 

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.

更新CloudKit记录有问题。我想要实现的是允许用户通过单击tableview行为一本书投票。这个表视图目前使用来自云数据的记录填充。当我想要检索一个记录时,我被卡住了。然后将voteCount列增加1,然后将其保存到云中,并重新加载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?

你能帮我更新一个特别的记录,然后把它送回云端吗?

1 个解决方案

#1


2  

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

分享到: