-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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