如何在不使GUI无响应的情况下进行Web服务调用

时间:2012-09-22 10:46:56

标签: iphone

我有UISearchBarUISearchDisplayController。当用户在searchBar:textDidChange:内写入文本时,我会进行Web服务调用以过滤我的TableView。问题是GUI在从Web服务获得结果之前没有响应。我试图使用[self performSelector:@selector(callWebService:) withObject:searchText];来解决它,但它仍然没有响应。

编辑:在Flink建议之后,我将performSelector更改为performSelectorInBackground,但现在tableView未正确过滤,它只显示“无结果”。 即使tableView:numberOfRowsInSection:也未被调用。

再次编辑:我得到'无结果'的原因是由于没有在正确的tableView上调用reloadDataUISearchDisplayController有一个名为searchResultsTableView的媒体资源。所以最后我使用的是[self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:false];,现在它工作正常。

应该注意的是,虽然我选择了performSelectorInBackground,但我可能应该尝试在sendAsynchronousRequest上使用NSURLConnection方法 - 请参阅AliSoftware的回答。

1 个答案:

答案 0 :(得分:1)

您需要让您的网络呼叫异步。

http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

在您的情况下,您可以将performSelector更改为performSelectorInBackground