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 ?
The default priority of Main thread is 5 and for all remaining user threads priority will be inherited from parent to child. Example. Java.
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).
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With