經過在yonel的解決方案中有些天真的複製,並稱它很好,我意識到調用performSelectorOnMainThread:withObject:waitUntilDone:
修復了症狀,但不是問題。更大的問題是,您仍然在異步或後臺線程的上下文中進行UI更新。
這就是看起來像我的代碼:
dispatch_queue_t queue = dispatch_queue_create("com.kyleclegg.myqueue", NULL);
dispatch_async(queue, ^{
// Make API call
// Retrieve data, parse JSON, update local properties
// Make a call to reload table data
});
當它應該是這樣的:
dispatch_queue_t queue = dispatch_queue_create("com.kyleclegg.myqueue", NULL);
dispatch_async(queue, ^{
// Make API call
// Retrieve data, parse JSON, update local properties
dispatch_async(dispatch_get_main_queue(), ^{
// Now make the call to reload data and make any other UI updates
[self.tableView reloadData]
});
});
如果您需要做的唯一的事就是調用[self.tableView reloadData]
它可能是罰款使用performSelectorOnMainThread:withObject:waitUntilDone:
,因爲它實現了相同的目標,但您也應該認識到大圖中發生的事情。此外,如果您正在執行更多的UI工作而不是重新加載表,那麼所有代碼也應該放在主隊列中。
參考:A concise example使用GCD和管理背景與主線程。
通常reloadData方法刷新tableview的視圖。你可以把你的TableViewController代碼的樣本? – 2010-02-23 23:32:23