在后台线程上search

我试图在我的iPhone应用程序中search几千个对象,但search滞后很多 – 在每次击键之后,UI冻结1-2秒。 为了防止这种情况,我必须在后台线程上执行search。

我想知道是否有人在后台线程上search一些技巧? 我在NSOperation读了一下,search了网页,但没有find有用的东西。

尝试在视图控制器中使用NSOperationQueue作为实例variables。

 @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