Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default thread

In iOS, we have GCD and Operation to handle concurrent programming.

looking into GCD we have QoS classes, and they're simple and straight forward, this question is about why DispatchQueue.main.async is commonly used to asynchronies X tasks in the Main Thread.

So when we usually handle updating something in the UI we usually use that function since to prevent any irresponsiveness from the application.

makes me think is writing code inside the UIViewController usually executed in the main thread ?

but also knowing that callback & completionHandler usually execute without specifying on what thread they are in, and the UI never had a problem with that !! so it is on the background ?

How Swift handles this ? and what thread am i writing on by default without specifying anything ?

like image 735
Mohmmad S Avatar asked Mar 06 '19 09:03

Mohmmad S


People also ask

What is default thread in Java?

The default priority of Main thread is 5 and for all remaining user threads priority will be inherited from parent to child. Example. Java.

What is the main thread?

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).

How do I find my main thread name?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

What is child thread in Java?

Basically, we can split the execution thread into two. After this, both threads execute concurrently. The creating thread is the parent thread, and the created thread is a child thread. Note that any thread, including the main program which is run as a thread when it starts, can create child threads at any time.


1 Answers

Since there are more than one question here, let's attempt to answer them one by one.

why DispatchQueue.main.async is commonly used to asynchronies X tasks in the Main Thread.

Before mentioning a direct answer, make sure that you don't have confusion of understanding:

  • Serial <===> Concurrent.
  • Sync <===> Async.

Keep in mind that DispatchQueue.main is serial queue. Using sync or async has nothing to do with determining serialization or currency of a queue, instead they refer to how the task is handled. Thus saying DispatchQueue.main.async means that:

returns control to the current queue right after task has been sent to be performed on the different queue. It doesn't wait until the task is finished. It doesn't block the queue.

cited from: https://stackoverflow.com/a/44324968/5501940 (I'd recommend to check it.)

In other words, async means: this will happen on the main thead and update it when it is finished. That's what makes what you said:

So when we usually handle updating something in the UI we usually use that function since to prevent any irresponsiveness from the application.

seems to be sensible; Using sync -instead of async- will block the main.


makes me think is writing code inside the UIViewController usually executed in the main thread ?

First of all: By default, without specifying which thread should execute a chunk of code it would be the main thread. However your question seems to be unspecific because inside a UIViewController we can call functionalities that are not executed on the main thread by specifying it.


but also knowing that callback & completionHandler usually execute without specifying on what thread they are in, and the UI never had a problem with that !! so it is on the background ?

"knowing that callback & completionHandler usually execute without specifying on what thread they are in" No! You have to specify it. A good real example for it, actually that's how Main Thread Checker works.

I believe that there is something you are missing here, when dealing when a built-in method from the UIKit -for instance- that returns a completion handler, we can't see that it contains something like DispatchQueue.main.async when calling the completion handler; So, if you didn't execute the code inside its completion handler inside DispatchQueue.main.async so we should assume that it handles it for you! It doesn't mean that it is not implemented somewhere.

Another real-world example, Alamofire! When calling

Alamofire.request("https://httpbin.org/get").responseJSON { response in
    // what is going on here work has to be async on the main thread
}

That's why you can call it without facing any "hanging" issue on the main thread; It doesn't mean its not handled, instead it means they handle it for you so you don't have to worry about it.

like image 200
Ahmad F Avatar answered Oct 17 '22 02:10

Ahmad F