I have a UISearchResultsController which is filtering my NSFetchedResultsController and putting the filtered data into an array. Currently, I use an NSPredicate to take the search bar content and apply the filter. Here's my predicate:
[filteredArray removeAllObjets];
for(Account *account in unfilteredResults){
NSPredicate *predicate;
if(controller.searchBar.selectedScopeButtonIndex == 0){
predicate = [NSPredicate predicateWithFormat:@"accountFirstName BEGINSWITH[cd] %@", searchString];
}else if(controller.searchBar.selectedScopeButtonIndex == 1){
predicate = [NSPredicate predicateWithFormat:@"accountLastName BEGINSWITH[cd] %@", searchString];
}else if(controller.searchBar.selectedScopeButtonIndex == 2){
predicate = [NSPredicate predicateWithFormat:@"group.groupName CONTAINS[cd] %@", searchString];
}
if([predicate evaluateWithObject:account]){
[self.filteredArray addObject:account];
}
}
I'm trying to filter the accounts based on either first name, last name, or group name. I know that these operations are slow, but what can be done to make them faster?
Edit:
I just noticed that I'm recreating the predicate every iteration, but I still think that there should be a better way. I did see something about doing a binary compare in an Apple video, but I have no idea how to convert a search string into a binary string and less how to get the "next greatest" value.
How do I replace the BEGINSWITH statement with something more efficient?
I'm assuming your data objects are CoreData objects, if so have you marked the properties you are searching on as indexed?