我正在我的应用中创建一个类型,可以访问远程休息服务。现在的问题是我使用的NSUrl连接是同步的,并且由于通过json对象循环的数量需要一些时间来解析。这导致UI在我搜索时冻结。
因此我决定尝试:
[NSURLConnection sendAsynchronousRequest:myRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *taxiData, NSError *error) {
它看起来好一点但仍然挂起,因为大多数处理是在完成处理程序中完成的,它与我假设的主队列绑定。
然后我尝试创建自己的NSOperationsQueue或使用dispatch_asynch:
[NSURLConnection sendAsynchronousRequest:myRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *taxiData, NSError *error) {
dispatch_queue_t typeAheadQueue = dispatch_queue_create("typeAhead", NULL);
dispatch_async(typeAheadQueue, ^ {
}];
它工作正常。唯一的问题是现在uitableview刷新数据需要10秒钟。我现在可以在没有任何中断的情况下打字,但更新uitableview会有很大的延迟。
我接近这个权利了吗?任何帮助表示赞赏。
谢谢!
答案 0 :(得分:1)
检查此主题:run a process in the background while user can still use the UI
考虑将整个nsurlconnection调用放在一个单独的队列中,当需要更新用户界面时,请使用像Mundi建议的get_main_queue。
答案 1 :(得分:0)
您可以使用
向主队列发送信号dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});