Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Created September 30, 2014 14:47
Show Gist options
  • Save ccabanero/e0f72d1067ba69eb996a to your computer and use it in GitHub Desktop.
Save ccabanero/e0f72d1067ba69eb996a to your computer and use it in GitHub Desktop.
CloudKit: How to query a record from a Container's Public Database Using NSPredicate (Swift)
//get the container for the App
let defaultContainer: CKContainer = CKContainer.defaultContainer()
//get the PublicDatabase inside the Container
let publicDatabase: CKDatabase = defaultContainer.publicCloudDatabase
//predicate for query
let predicateForAddress = NSPredicate(format: "address = '123 Beggers Canyon, Tatooine'", argumentArray: nil)
//create query
let query = CKQuery(recordType: "POI", predicate: predicateForAddress)
//execute query
publicDatabase.performQuery(query, inZoneWithID: nil) { (resultsArray, queryError) -> Void in
if queryError != nil {
println("Uh oh, there was an error querying ...")
println(queryError.localizedDescription)
}
if resultsArray != nil {
for result in resultsArray {
let record: CKRecord = result as CKRecord
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