Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active August 29, 2015 14:07
Show Gist options
  • Save ccabanero/a78827f0fa13ac498a14 to your computer and use it in GitHub Desktop.
Save ccabanero/a78827f0fa13ac498a14 to your computer and use it in GitHub Desktop.
CloudKit: How to query a record from a Container's Public Database Using NSPredicate (Objective-C)
//get the Container for the App
CKContainer *defaultContainer = [CKContainer defaultContainer];
//get the PublicDatabase inside the Container
CKDatabase *publicDatabase = [defaultContainer publicCloudDatabase];
//predicate for query
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"address = '123 Beggers Canyon, Tatooine'", nil];
//create query
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"POI" predicate:predicate];
//execute query
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
//handle query error
if(error) {
NSLog(@"Uh oh, there was an error querying ... %@", error);
} else {
//handle query results
if([results count] > 0) {
//iterate query results
for(CKRecord *record in results) {
NSLog(@"Query was successfully");
NSLog(@"Title: %@", record[@"title"]);
NSLog(@"Description: %@", record[@"description"]);
NSLog(@"Address: %@", record[@"address"]);
NSLog(@"Locations: %@", record[@"locations"]);
}
//handle no query results
} else {
NSLog(@"Query returned zero results");
}
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment