Updated 29 September 2021
Multithreading in iOS using GCD is a technique to perform multiple processes simultaneously.
Multithreading is a way to perform two or more tasks at the same time.
In Multithreading CPU switches to each task and performs the operations.
Every application has at least one thread called the main thread.
Multithreading in iOS using GCD makes the application performance very fast and smooth.
Multicore processors allow us to perform multiple tasks in a parallel way.
We will use GCD to perform the multithreading.
According to apple
GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects.
Work submitted to dispatch queues is executed on a pool of threads fully managed by the system.
GCD manages the thread pool and you don’t have to worry about hardware resources with concurrent code.
GCD work on a system level and it’s more aware of resources.
GCD usages three types of queues to perform the multithreading.
Mani thread has top priority. in this thread we update our UI part in the application.
This thread makes our UI load very fast and smooth and gives better scrolling.
1 2 3 4 |
DispatchQueue.main.async { //Perform ui related update } |
Global queue shared by the whole system. The global queue has 5 different types of priority queues.
1-> userInteractive: This is a user-interactive queue means here we will update UI and this queue has the highest priority.
1 2 3 |
DispatchQueue.global(qos: .userInteractive).async { //perform operation } |
2-> userIntiated: This queue has low priority compared to the above type.
1 2 3 |
DispatchQueue.global(qos: .userInitiated).async { //perform operation } |
3-> default: don’t use it usually, the type will be inferred by the system.
1 2 3 |
DispatchQueue.global(qos: .default).async { //perform operation } |
4-> utility: this queue can take few seconds to few minutes.
1 2 3 |
DispatchQueue.global(qos: .utility).async { //perform operation } |
5-> background: this will take minutes to hours to perform the operation.
1 2 3 |
DispatchQueue.global(qos: .background).async { //perform operation } |
Custom queue created by own. we can create one or more custom queues with different QoS and attributes.
1 2 3 |
let concurrentQueue = DispatchQueue(label: "concurrent", qos: .background, attributes: .concurrent) let serialQueue = DispatchQueue(label: "serialQueue", qos: .background) |
Now we will take one example with the use of the GCD.
Here we will use two types of queue with different types of priority.
We are using loops in the global queue with different types of priority.
1 2 3 4 5 6 7 8 9 10 11 12 |
DispatchQueue.global(qos: .background).async { for index in 0..<10 { print("😊", index) } } DispatchQueue.global(qos: .userInteractive).async { for index in 0..<10 { print("❤", index) } } } |
here you will see userInteractive output first than background queue and multiple threads working.
And thanks for reading this blog, for detail info please click here
For more blogs please click here.
So pls follow the above step and And if you have any issues or suggestions you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.