Created
January 27, 2017 15:13
-
-
Save stevenhuey/4d1e67af1b64bf0722c8553fb32272e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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