2011-10-15 26 views
1

我有一个内部有2个视图的UIView,一个是关于我们的页面,另一个是通过uisegmentation控制的twitter流/页面。如何在performSelectorInBackground后更新UItableview?

Twitter feed在didFinishLaunchingWithOptions上运行,并在后台运行。

在Twitter页面本身,我有一个重新加载按钮,启动相同的过程,再次在后台执行。

我坚持,因为表视图从来没有更新,即使

[self.tableView reloadData]; 
的performInSelector后直

因此我想要执行一次该表的数据的更新:

[自performSelectorInBackground:@selector(reloadTwitter :) withObject:无];

完成。

我该如何做这样的工作?

回答

4

第一个答案可能会工作,但你可能不会有兴趣使用GCD和块。总的来说,真正的问题很可能是你不应该试图在后台线程中更新任何用户界面元素 - 你必须从主线程中完成。

所以,你最好的选择是很可能被刷新Twitter的饲料的方法中添加另一行:

[self.tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:No]; 

苹果的文档在这个位置:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/AboutThreads/AboutThreads.html#//apple_ref/doc/uid/10000057i-CH6-SW2

检查节标记为“线程和您的用户界面”。

+0

我不确定在哪里把performSelectorOnMainThread,但这当然有帮助!这是个好主意! – zardon

+0

您需要将它放在您的后台运行方法的末尾并更新Twitter提要。基本上在你有新数据显示的地方。 – Carter

2

使用GCD和块为... :)

/* get a background queue (To do your things that might take time) */ 
dispatch_queue_t backgroundQueue = 
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
/* get the main queue (To update the UI)*/ 
dispatch_queue_t mainQueue = dispatch_get_main_queue(); 

/* use dispatch_async to run something (twitter, etc) 
    asynchronously in the give queue (in the background) */ 
dispatch_async(backgroundQueue,^{ 
    [self reloadTwitter]; 
    /* use again dispatch_async to update the UI (the table view) 
    in another queue (the main queue) */ 
    dispatch_async(mainQueue,^{ 
    [self.tableView reloadData]; 
}); 
}); 
+0

听起来很有趣,我会给它一个去,谢谢 – zardon