开发者

Display of results is delayed by 5s in UISearchDisplayController

开发者 https://www.devze.com 2023-02-09 16:57 出处:网络
My UISearchDisplayController performs asynchronous searches via NSOperationQueue. However the results table does not visually update until approximately 5s after the NSOperation calls [searchDisplayC

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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消