开发者

How to update a UItableview after performSelectorInBackground?

I have a UIView with 2 views inside it, one is an about us page, the other is a twitter stream/page controlle开发者_C百科d via a uisegmentation.

The twitter feed runs on didFinishLaunchingWithOptions and runs in the background.

On the twitter page itself I have a reload button which launches the same process, again performing in the background.

I am stuck because the table view never updates, even with

[self.tableView reloadData];

straight after the performInSelector.

Thus I am wanting to perform an update of the table's data once:

[self performSelectorInBackground:@selector(reloadTwitter:) withObject:nil];

is finished.

How do I do such a task?


The first answer would probably work, but you may not be interested in using GCD and blocks. Overall the real problem is most likely that you should not be attempting to update any user interface elements in a background thread - you must do it from the main thread.

So your best option is to probably add another line within the method that is refreshing the twitter feed:

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

Apple has documentation on this here:

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

Check the section labeled "Threads and Your User Interface".


use GCD and blocks for that... :)

/* 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜