UISearchDisplayController 中结果显示延迟 5 秒
我的 UISearchDisplayController 通过 NSOperationQueue 执行异步搜索。
但是,直到 NSOperation
调用 [searchDisplayController.searchResultsTableView reloadData]
大约 5 秒后,结果表才会以可视方式更新。
- (BOOL) searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
[searchQueue cancelAllOperations];
NSInvocationOperation *op = [[[CustomSearchOperation alloc] initWithController:controller searchTerm:searchString] autorelease];
[searchQueue addOperation:op];
return NO;
}
我的 CustomerSearchOperation 更新 tableView,如下所示:
- (void) main
{
// perform search
[searchDisplayController setContents:results];
[searchDisplayController.searchResultsTableView reloadData];
}
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 search
[searchDisplayController setContents:results];
[searchDisplayController.searchResultsTableView reloadData];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 UI 更新必须在主线程上进行,并且通过 NSOperationQueue 从后台线程调用
reloadData
。您可以使用
NSObject
方法 performSelectorOnMainThread:withObject:waitUntilDone: 以确保此类更新发生在主线程上。The problem is that UI updates must occur on the main thread, and
reloadData
is being called from a background thread via theNSOperationQueue
.You can use the
NSObject
method performSelectorOnMainThread:withObject:waitUntilDone: to ensure such updates occur on the main thread.