GCD (Grand central dispatch)
Synchronous and Asynchronous queue
GCD (Grand central dispatch)
Synchronous and Asynchronous queue
We use synchronous when we want to execute one task at a time.
Asynchronous queue means multiple tasks execute at a time, regardless of the other finishing time.
Dispatch Queue
An object that manages the execution of tasks serially or concurrently on your app’s main thread or background thread.
Dispatch queues are FIFO queue to which your application can submit tasks as block objects. Work submitted to dispatch queues executes as a pool of threads managed by the system.
Global and Main thread
Global queue is dispatched on main queue as sync, the dispatched task will work on the same thread of main queue and dispatched task is added to global queue, thus freezing the thread.
If the global queue is dispatched on the main queue as async, the dispatch task will work on another thread of the main queue, and this task will not freeze the thread.
Main queue
If the main queue is dispatched on the main queue as sync, will make an exception because of a deadlock.
If the main queue is dispatched in the main queue as async, the dispatched task will work on the same thread of the main queue.
Dispatch Work Item
Dispatch work item encapsulates work to be performed on a dispatch queue or in a dispatch group. We primarily used it in scenarios where we require the capability of delaying or cancelling a block of code from executing.
Dispatch Group
With dispatch group, we can group together multiple tasks and either wait for them to be completed or notified once they are completed. Tasks can be synchronous or asynchronous and can even run on different queues.
-
Using wait() and then execute completion block on main queue
-
call group notify()
Dispatch barrier
A dispatch barrier allows us to create a synchronization point within a concurrent dispatch queue. In normal operation, the queue acts just like a normal concurrent queue. but when the barrier is executing, it acts like a serial queue. After the barrier finishes, the queue goes back to being a normal concurrent queue.
It allows you to make a thread-unsafe object to thread safe. One frequent concern with singletons is that often they’re not thread safe as singleton are often used from multiple controllers accessing the singleton instance at the same time. Using barrier in a custom concurrent queue is a good choice for handling thread safety in critical areas of code.
Semaphore
Semaphore, which is a very significant technique for managing concurrent processes by using a simple integer value. Semaphore is simply an integer value that is shared between threads. This variable is used to solve the critical section problem and to achieve process synchronization.
A semaphore is useful when you want to set a maximum on the number of threads operating on the same shared resource at a time. One common use is when the maximum is 1 because the shared resource requires exclusive access.
A semaphore has a value which is a non-negative integer and two operations:-
-
wait if the value is not zero, decrement it, otherwise block until something signals the semaphore.
-
signals if there are threads waiting, unblock one of them, otherwise increment the value.
QoS (Quality of service)
A QoS class categorizes work to perform on a dispatch queue, by specifying the quality of a task, you shows its importance to your app. When scheduling tasks, the system prioritizes those that have higher classes.
QoS can be used with .async() function, and the priorities can be divided into four main categories:-
-
User interactive:- Used for animation, or updating UI.
-
User Initiated:- Used for tasks like loading data from API, preventing the user from making interactions.
-
utility:- Used for tasks that do not need to be tracked by the user.
-
background: Used for tasks like saving data in the local database or any maintenance code which is not on high priority.
Async and Await
Await is the keyword to be used for calling async methods. Marking async functions with the new async keyword, then calling them using the await keyword.
Operations
Operations are an object-oriented way to encapsulate work that you want to perform asynchronously. Operations are designed to be used either in conjunction with an operation queue or by themselves.
The operation class itself is an abstract base class that must be subclassed in order to do any useful work. Despite being abstract, this class does provide a significant amount of infrastructure to minimize the amount of work you have to so in your own subclasses.
There are 3 ways to create the operations:-
- Block operation (Concrete class) :- Operation objects execute in a synchronous manner by default — that is, they perform their task in the thread that calls their start method
- Works on the main thread
Since we call start method on other thread, it will block that thread.
Please visit this git hub page for finding out more illustrations:
메타데이터
- post_id
- 437b418a2f80
- slug
- understanding-queue-in-ios-437b418a2f80
- url
- https://medium.com/@monicadeswal01/understanding-queue-in-ios-437b418a2f80
- canonical_url
- https://medium.com/@monicadeswal01/understanding-queue-in-ios-437b418a2f80
- author_url
- https://medium.com/@monicadeswal01
- status
- ok
- fetched_at
- 2026-06-17 12:55:42