Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to update UI in main thread or in main queue?

I read a lot many articles which specifies that we need to update the UI in the main thread however whenever I update my UI is always the code.

DispatchQueue.main which in return gives me the Queue not thread. How exactly I would access the thread or both are same ?

like image 681
Dipesh Pokhrel Avatar asked Jul 05 '18 10:07

Dipesh Pokhrel


People also ask

Why do you have to update the UI on the main thread?

It has been in the loop of constantly processing events and hibernation to ensure that user events can be responded as soon as possible. The reason why the screen can be refreshed is because `Main Runloop` is driving. Assume we use our Magical UIKit, and update UI on background threads.

Can we update UI from thread?

Worker threads However, note that you cannot update the UI from any thread other than the UI thread or the "main" thread. To fix this problem, Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help: Activity.

Is the main thread the same as the UI thread?

UI Thread and Main Thread are same only in Android. The Main thread, that is responsible for handling the UI events like Draw, Listen and receive the UI events. Ans also it is responsible for interact with running components of the UI toolkit for the corresponding application that belongs to.

What are different ways of updating UI from background thread?

In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });


3 Answers

Imagine a train station where the number of the train is the same as the number of the platform that it leaves from.

So if you want the #1 train, you stand on #1 platform. You cannot get on the train without first standing on the platform. Everyone else who wants to get on this train also stands on the platform to wait for their chance to get on the train.

The train is the thread. The platform is the queue.

If you want to get on the main thread, get on the main queue.

like image 156
matt Avatar answered Oct 19 '22 14:10

matt


From Dispatch Queues in the Concurrency Programming Guide:

Main dispatch queue

The main dispatch queue is a globally available serial queue that executes tasks on the application’s main thread. This queue works with the application’s run loop (if one is present) to interleave the execution of queued tasks with the execution of other event sources attached to the run loop. Because it runs on your application’s main thread, the main queue is often used as a key synchronization point for an application.

Generally, GCD maintains a pool of threads, and there is no 1-1 relationship between dispatch queues and threads. But the main queue is special: it is tied to the main thread, all items dispatched to the main queue are executed on the main thread. (The same is true for OperationQueue.main.)

Dispatching code to DispatchQueue.main (or OperationQueue.main) ensures that it is executed on the main thread, and synchronized with other UI updates.

In this sense, the terms “execute on the main thread” and “execute on the main queue” are often used interchangeably.

like image 35
Martin R Avatar answered Oct 19 '22 16:10

Martin R


DispatchQueue manages the execution of the code on a specific thread.

From Apple documentation:

DispatchQueue manages the execution of work items. Each work item submitted to a queue is processed on a pool of threads managed by the system.

So, when you call

DispatchQueue.main.async {
        //your code
}

This code is submitted to the main queue which in turn runs on the Main thread.

like image 25
UditS Avatar answered Oct 19 '22 14:10

UditS