views:

397

answers:

1

Much of my code is based off of Apple's TableSearch example, however my app contains 35,000 cells that need to be searched rather than the few in the example. There isn't much documentation online about UISearchDisplayController since it is relatively new. The code I am using is as follows:

- (void)filterContentForSearchText:(NSString*)searchText {
/*
 Update the filtered array based on the search text and scope.
 */

[self.filteredListContent removeAllObjects]; // First clear the filtered array.

/*
 Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
 */
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (Entry *entry in appDelegate.entries)
{
 if (appDelegate.searchEnglish == NO) {
  NSComparisonResult result = [entry.gurmukhiEntry compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
  if (result == NSOrderedSame)
  {
   [self.filteredListContent addObject:entry];
  }
 }
 else {
  NSRange range = [entry.englishEntry rangeOfString:searchText options:NSCaseInsensitiveSearch];
  if(range.location != NSNotFound)
  {
   [self.filteredListContent addObject:entry];
        }

 }
}}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
[self.view bringSubviewToFront:keyboardView];

// Return YES to cause the search result table view to be reloaded.
return YES;}

My problem is that there is a bit of a delay after each button is pressed on the keyboard. This becomes a usability issue because the user has to wait after typing in each character as the App searches through the array for matching results. How can this code be adjusted so that the user can continually type without any delays. In this case, it is ok for a delay in the time it takes for the data to reload, but it should not hold up the keyboard in typing.

A: 

Update:

One way to acccomplish searchching while typing without "locking up" the UI, is to use threads.

So you can call the the method that performs the sorting with this method:

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

Which will keep it out of the main thread, allowing the UI update.

You will have to create and drain your own Autorealease pool on the background thread.

However, when you want to update the table you will have to message back to the main thread (all UI updates MUST be on the main thread):

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

You could also get a little more control by using NSOperation/NSOperationQueue Or NSThread.

Note that implementing threads is fraught with peril. You will have to make sure your code is thread-safe and you may get unpredictable results.

Also, here are other stackoverflow answers that may help:

http://stackoverflow.com/questions/1066590/using-nsthreads-in-cocoa/1066773#1066773

http://stackoverflow.com/questions/1004845/where-can-i-find-a-good-tutorial-on-iphone-objective-c-multithreading/1008991#1008991


Original answer:

Don't perform the search until the user presses the "search" button.

There is a delegate method you can implement to catch the pressing of the search button:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
Corey Floyd
not exactly what I am looking for...I have seen it done before where the user types and there is a delay in loading of the table but it is separate from the keyboard which could freely type for the large database.
Kulpreet
I just upated my answer to something that may be more relevant
Corey Floyd