iOS: Running Code on the Main Thread

Problem:

In iOS, updating the user interface should be done in the main thread. However, many asynchronous methods invoke the callback on an arbitrary thread. How can I make sure a particular piece of code is run in the main thread?

Solution:

You need to use the grand central dispatch:

dispatch_async(dispatch_get_main_queue(),^{
    // The code to run on the main thread goes here
    [self.tableView reloadData];
});

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.