Equivalent for Activity.runOnUiThread(Runnable) in iOS?

951 views
Skip to first unread message

blueri...@gmail.com

unread,
Jan 22, 2014, 1:30:01 PM1/22/14
to rob...@googlegroups.com
Hi,

I'm using lot of networking in my app. When a networking thread is finished I need to update the UI. Sometimes this works, sometimes it fails and I assume that is because I'm updating the UI from the networking thread.
On Android in can update my UI from networking via runOnUiThread(Runnable) and then never get any crashes. 

But I don't know what's the equivalent for this in iOS. Does someone know how I can run code on the ui thread?

Thanks!

blueri...@gmail.com

unread,
Jan 22, 2014, 8:25:08 PM1/22/14
to rob...@googlegroups.com
I think that dispatch_async runs block code on the main thread:

dispatch_async(dispatch_get_main_queue(), ^{

   // runs on ui thread.

});


But how can I bind this method?

CremaGames Studios

unread,
Jan 23, 2014, 2:29:26 AM1/23/14
to rob...@googlegroups.com
You can use NSOpeartionQueue. Here are the bindings for it:


And here's how to use it in your code:

NSOperationQueue.getMainQueue().addOperationWithBlock(new VoidBlock() {
@Override
public void invoke(){
doSomethingInBackground();
}
});

blueri...@gmail.com

unread,
Jan 23, 2014, 10:30:32 AM1/23/14
to rob...@googlegroups.com
Cool, Thanks!!  I will try it out :)
Reply all
Reply to author
Forward
0 new messages