Search on topic background

I'm trying to search several thousand objects in an application for iPhone, but the search is far behind - after each keystroke, the user interface freezes for 1 - 2 seconds. To prevent this, I have to do a search in the background thread.

I was wondering if anyone has any tips on searching in the background thread? I learned a little from NSOperation and searched the Internet, but did not find anything useful.

+6
multithreading ios thread-safety objective-c
source share
1 answer

Try using NSOperationQueue as an instance variable in your view controller.

 @interface SearchViewController : UIViewController { NSOperationQueue *searchQueue; //other awesome ivars... } //blah blah @end @implementation SearchViewController - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { if((self = [super initWithNibName:nibName bundle:nibBundle])) { //perform init here.. searchQueue = [[NSOperationQueue alloc] init]; } return self; } - (void) beginSearching:(NSString *) searchTerm { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //perform search... [self.searchDisplayController.searchResultsTableView reloadData]; [pool drain]; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText /* Cancel any running operations so we only have one search thread running at any given time.. */ [searchQueue cancelAllOperations]; NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(beginSearching:) object:searchText]; [searchQueue addOperation:op]; [op release]; } - (void) dealloc { [searchQueue release]; [super dealloc]; } @end 
+6
source share

All Articles