GCD provides a set of global dispatch queues that are managed by the system. These queues are categorised into different quality-of-service (QoS) classes, indicating their priority.
userInteractive
: Highest priority, used for tasks that must be executed immediately to provide a smooth user experience.userInitiated
: High priority, used for tasks initiated by the user that require immediate results.default
: Default priority, used for tasks that are not time-critical but should be executed reasonably soon.utility
: Low priority, used for long-running tasks that are not time-sensitive, such as file I/O or network requests.background
: Lowest priority, used for tasks that can run in the background, such as data synchronisation or prefetching.


Main Dispatch Queue:
The Main Dispatch Queue is a special serial dispatch queue associated with the main thread of your application. It’s the primary queue for updating the user interface and handling events like user interactions.



print("1")
is executed synchronously, printing “1” immediately.DispatchQueue.main.async
is called to asynchronously execute the closure on the main queue.print("5")
is executed synchronously after the async call, printing “5”.- Inside the async closure:
print("2")
is executed synchronously, printing “2”.DispatchQueue.main.async
is called again to asynchronously execute the inner closure on the main queue.print("4")
is executed synchronously after the async call, printing “4”.
- Inside the inner async closure:
print("3")
is executed synchronously, printing “3”.


print("1")
is executed synchronously, printing “1” immediately.DispatchQueue.main.async
is called to asynchronously execute the closure on the main queue.print("5")
is executed synchronously after the async call, printing “5”.- Inside the async closure:
print("2")
is executed synchronously, printing “2”.DispatchQueue.global().async
is called to asynchronously execute the closure on the global queue.print("4")
is executed synchronously after the async call, printing “4”.
print("3")
is not guaranteed to be executed before “4” because it’s scheduled asynchronously on the global queue, which may take some time to execute.


Explaination:
- The outer block (print2) will execute first because it’s dispatched before the inner block (Print1).
- The inner block (Print1) will execute after the outer block because it’s nested within it.

Dispatch.main is a serial queue which has single thread to execute all the operations. If we call sync on this queue it will block all other operations currently running on the thread and try to execute the code block inside sync whatever you have written. This results in “deadlock” and app will get crash.
Dispatch Groups
Dispatch groups provide a way to monitor the completion of multiple tasks dispatched asynchronously. They allow you to wait until all tasks in the group have finished executing before continuing with further code.

