How to Keep Updating Cloud Kit Record in Swift

How to update a field on a Record Type in CloudKit?

Only realized now where is the problem:

publicData.perform(query, inZoneWith: nil) { (results: [CKRecord]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
}

if let users = results {
self.ctUsers = users
print("\nHow many users in cloud: \(self.ctUsers.count)\n")

if self.ctUsers.count != 0 {

let user = users.first //CKRecord(recordType: "Elder")
user?["careTakerId"] = ctid

let publicData = CKContainer.default().publicCloudDatabase
publicData.save(user, completionHandler: { (record: CKRecord?, error: NSError?) in
if error == nil {

DispatchQueue.main.asynchronously(execute: { () -> Void in
print("CARETAKER updated successfully\n")
})
}
else {
print("\nHUEHUEHUEHUE\n")
print(error?.localizedDescription)
}
})
}
else {
print("\nELDER ISN'T IN CLOUD\n")
print(error?.localizedDescription)
}
}
}

I was literally creating a new record! I only needed to access the results variable, that is a vector of CKRecord and then change and save the field!

SWIFT/CloudKit - Updating a record and saving it back to container

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

CloudKit how to modify existing record (swift 3)

When you call the query, you get an array of CKRecord objects. Use the subscript to edit the record:

record["key"] = value as CKRecordValue

when you're finished, take the CKRecord and use either CKModifyRecordsOperation or CKDatabase.save(_:completionHandler:) to save it back to the server.

How to update the CloudKit schema after the app has been released to the AppStore

In my case, I didn't even need to run the initializeCloudKitSchema() method. Here is what I did that worked for me.

  1. I tested locally with two devices and made sure everything was syncing as expected. This of course was done in Xcode within the sandbox environment using testing login accounts.

  2. Then I went to the development CloudKit container and clicked on the Deploy Schema Changes.

  3. Lastly I went and downloaded the app directly from the App Store on two different devices using a production/regular user account and tested it. Everything worked as expected.

  4. Done

In theory, it looks like you need to deploy the schema to the Production CloudKit container once you're satisfied with the schema and the results in the testing environments by doing the above or possibly calling the initializeCloudKitSchema() which I didn't try.

Side notes: It looks like once you deploy your schema to the Production CloudKit container you no longer can delete or rename Entities or Attributes. Also, you will have to do the above every time you update the Core Data schema. Keep in mind that if you add or remove Entities or Attributes in Core Data, you must create a new version of the Core Data container to do what is called a light migration.

How to update CKRecord by CKModifyRecordsOperation?

I found the solution.

Just set CKModifyRecordsOperation operation's save policy to changedKeys.



Related Topics



Leave a reply



Submit