user3732539 user3732539 - 1 month ago 10
iOS Question

iOS application have multiple main thread

Can we create multiple main thread in single application?
Why all UI changes made in main thread?

I have faced these question in interviews. I am unable to find any solution. Can any one help me to find the answers of this question? That will help me in future interviews and implementations in iOS app.

Thanks in advance!


No, there is only one main thread for the application that is created by system for application. Main thread is the thread that directly gives changes to user. Thread which is directly connect with user you can said.

Check the Apple Documentation, It states,

The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:

  • Calling dispatch_main

  • Calling UIApplicationMain (iOS) or NSApplicationMain (OS X)

  • Using a CFRunLoopRef on the main thread

As with the global concurrent queues, calls to dispatch_suspend, dispatch_resume, dispatch_set_context, and the like have no effect when used with queues returned by this function.

Now, let's take an example, If you are downloading an image and want to display it to user. Now if you will download it on main thread then it will block other UI till it completed download. So, you should not download it on main thread or synchronously . you should download it asynchronously and when download is completed, you should display it to user on main thread, so it will quick displayed to user after download is completed.