Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:54:59+00:00 2026-06-12T17:54:59+00:00

In Android there are several ways to run some code in the main thread

  • 0

In Android there are several ways to run some code in the main thread from others ones:

 1. Activity.runOnUiThread(Runnable r)
 2. new Handler.post(Runnable r);
 3. View.post

What are the analogues in iOS?

 dispatch_async(dispatch_get_main_queue(), ^{

    });

Something else?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:55:00+00:00Added an answer on June 12, 2026 at 5:55 pm

    The preferred way nowadays is using GCD, with the code you quoted in your question:

    dispatch_async(dispatch_get_main_queue(), ^{
        // Your code to run on the main queue/thread
    });
    

    If you prefer using a more Object-Oriented approach than GCD, you may also use an NSOperation (like an NSBlockOperation) and add it to the [NSOperationQueue mainQueue].

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // Your code to run on the main queue/thread
     }];
    

    This does quite the same thing as dispatch_async(dispatch_get_main_queue(), …), has the advantage of being more Objective-C/POO oriented that the plain C GCD function, but has the drawback of needing to allocate memory for creating the NSOperation objects, whereas you can avoid it using plain C and GCD.


    I recommend using GCD, but there are other ways, like those two which allow you to call a selector (method) on a given object from the main thread:

    • - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait (method of NSObject so it can be called on any object)
    • Send - (void)performSelector:(SEL)aSelector target:(id)target argument:(id)anArgument order:(NSUInteger)order modes:(NSArray *)modes on the [NSRunLoop mainRunLoop]

    But those solutions are not as flexible as the GCD or NSOperation ones, because they only let you call existing methods (so your object has to have a method that already exists and does what you want to perform), whereas GCD or -[NSOperationQueue addOperationWithBlock:] allows you to pass arbitrary code (using the block).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an Android equivalent of didUpdateHeading (from iOS) ? Any sample code to
I am working on some Android animation effects. On one screen there are several
According to this answer or the android's documentation there is several ways to get
On Android there's neat way to browse between several pages of data with the
Is there any android-compatible (from 2.3.3 to 4.0.3) IRC library for Android ? As
In an android Activity is there a way to programmatically list the it has
There are several VPN Client applications on iOS and Android environments. Is there any
Currently, I know there are several official Android styles. @android:style/Theme.Black @android:style/Theme.Light @android:style/Theme.Translucent But I
I have read it on the official website that there are several ways in
On the iPhone there are several ways to detect when user opens the version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.