经过在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