Display of results is delayed by 5s in UISearchDisplayController
My UISearchDisplayController
performs asynchronous searches via NSOperationQueue
.
However the results table does not visually update until approximately 5s after the NSOperation
calls [searchDisplayController.searchResultsTableView reloadData]
.
- (BOOL) searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
[searchQueue cancelAllOperations];
NSInvocationOperation *op = [[[CustomSearchOperation alloc] initWithController:controller searchTerm:searchString] autorelease];
[searchQueue addOperation:op];
return NO;
}
My CustomerSearchOperation updates the tableView like so:
- (void) main
{
// perform searc开发者_C百科h
[searchDisplayController setContents:results];
[searchDisplayController.searchResultsTableView reloadData];
}
The problem is that UI updates must occur on the main thread, and reloadData
is being called from a background thread via the NSOperationQueue
.
You can use the NSObject
method performSelectorOnMainThread:withObject:waitUntilDone: to ensure such updates occur on the main thread.
- (void) main
{
// perform search
[sdc setContents:results];
[sdc.searchResultsTableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO];
}
精彩评论