Skip to content

Instantly share code, notes, and snippets.

@stevenhuey
Created Jan 27, 2017
Embed
What would you like to do?
- (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