Skip to content

Instantly share code, notes, and snippets.

@stevenhuey
Created January 27, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenhuey/4d1e67af1b64bf0722c8553fb32272e1 to your computer and use it in GitHub Desktop.
Save stevenhuey/4d1e67af1b64bf0722c8553fb32272e1 to your computer and use it in GitHub Desktop.
- (void)searchFor:(NSString*)query
{
// Cancel any existing search queries
if (self.searchQuery)
{
[self.searchQuery cancel];
self.searchQuery = nil;
[self.searchResults removeAllObjects];
[self updateSearchResults];
}
// Create the query
NSString* queryString = [NSString stringWithFormat:@"displayName == '*%@*'c", query];
self.searchQuery = [[CSSearchQuery alloc] initWithQueryString:queryString
attributes:@[@"displayName"]];
__weak typeof(self) weakSelf = self;
// When items are found, add them to the search results
self.searchQuery.foundItemsHandler = ^(NSArray<CSSearchableItem *> *items) {
for (CSSearchableItem* item in items)
{
ALAirport* airport = [weakSelf.airportController
airportWithIdentifier:[item uniqueIdentifier]];
[weakSelf.searchResults addObject:airport];
[weakSelf updateSearchResults];
}
};
// When complete, update the search results
self.searchQuery.completionHandler = ^(NSError* error) {
if (error)
{
NSLog(@"%@", error);
}
else
{
[weakSelf updateSearchResults];
}
};
// Start the search. Handle results and errors using the handler blocks
[self.searchQuery start];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment