1

I'm implementing a search function using a split view controller on iPad. It searches an array of NSDictionary I get from a JSON object.

I have added a search bar to the table view's header view and am using the code below to update the table with search results as the user is typing a search query into the search bar.

I'm interested if what I'm doing is the correct way to implement "search as you type" behavior on iOS? Will I run into performance issues if my JSON returns 1000 results? Do I need to implement a Search Results Controller instead of just putting the search bar into a table view header?

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self doSearchWithString:searchText];
}


- (void)doSearchWithString:(NSString*)searchString
{
    NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *object, NSDictionary *bindings) {
                                  //get the value from the dictionary (the name value)
                                  NSString *name = [object objectForKey:@"name"];

                                  if([[name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound) {
                                      return YES;
                                  }
                                  return NO;
                              }];

//change the datasource and ask the table to refresh itself
    self.searchResults = [self.dataSource filteredArrayUsingPredicate:predicate];
    [self.tableView reloadData];
}
Alex Stone
  • 46,408
  • 55
  • 231
  • 407

1 Answers1

4

Depending on the dataset size, you may struggle to get adequate performance with Core Data.

It might be worth pre-fetching then try a binary array search. See:

How to perform binary search on NSArray?

Otherwise, it may require a custom binary index such as a Directed Acyclic Word Graph. Take a look at this answer:

auto complete a search

Community
  • 1
  • 1
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132