Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Created September 30, 2014 13:45
Show Gist options
  • Save ccabanero/838c1cfc48924f6b9eca to your computer and use it in GitHub Desktop.
Save ccabanero/838c1cfc48924f6b9eca to your computer and use it in GitHub Desktop.
CloudKit: How to fetch a record from a Container's Public Database with a known Record Id (Swift)
//get the container for the App
let defaultContainer: CKContainer = CKContainer.defaultContainer()
//get the PublicDatabase inside the Container
let publicDatabase: CKDatabase = defaultContainer.publicCloudDatabase
//create the target record id you will use to fetch by
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 {
println("Fetched record by Id 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