Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Created September 30, 2014 14:13
Show Gist options
  • Save ccabanero/7b238941efb69e0d891d to your computer and use it in GitHub Desktop.
Save ccabanero/7b238941efb69e0d891d to your computer and use it in GitHub Desktop.
CloudKit: How to update a record in a Container's Public Database (Swift)
//get the container for the App
let defaultContainer: CKContainer = CKContainer.defaultContainer()
//get the PublicDatabase inside the Container
let publicDatabase: CKDatabase = defaultContainer.publicCloudDatabase
//create a record id for the target record we want to fetch then update
let wellKnownID: CKRecordID = CKRecordID(recordName: "1")
//fetch the target record using it's record id
publicDatabase.fetchRecordWithID(wellKnownID, completionHandler: { (record, error) -> Void in
if error != nil {
println("Uh oh, there was an error fetching...")
println(error.localizedDescription)
}
if record != nil {
//update a property
record.setObject("123 Beggers Canyon, Tatooine", forKey: "address")
//save the update back to the target database
publicDatabase.saveRecord(record, completionHandler: { (updateRecord, updateError) -> Void in
if updateError != nil {
println("Uh oh, there was an error updating...")
println(error.localizedDescription)
}
if updateRecord != nil {
println("Updated record Successflly")
println(record.objectForKey("title"))
println(record.objectForKey("description"))
println(record.objectForKey("address"))
println(record.objectForKey("location"))
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment