Dispatch Queue in Swift

Agnel Selvan
4 min readJan 6, 2022

--

Dispatch Queue is used for managing the concurrent operations. It is very useful in managing the asynchronous task in the background thread rather than running on the main thread which really helps in maintaining the performance of the app. Before diving into Dispatch Queue, let's first quickly have a look at Concurrency and threading.

Threading

Threading is a sequence of instructions to be executed on the runtime. Moreover, in iOS, In the main thread, the UI is rendered and managed. Threading provides a way to improve the app's performance by the method called parallelism means executing multiple instructions at once.

Concurrency

Concurrency means managing multiple queues of instructions at once. In iOS, using this the User Interface stays responsive as we are performing a set of instructions in a background thread. As the main thread is more specific for UI updation.

Queues

Now here in queues, a set of work will be assigned to queues and will be passed to Dispatch Queue for executing the work. There are two types of queues namely Serial Queue and Concurrent Queue.
In Serial Queue, the set of queues will be executed one after other whereas,
In Concurrent Queue, the multiple queues will be allowed to run at the same time.

Now let's start with a quick demo,

Serial Queue Asynchronous task

Task will run in a different thread when you perform the async tasks. During the async task, the task will not wait until the block gets executed it will directly start to execute the next block in a different thread.

Serial Queue Synchronus task

The synchronous task will wait until the block executes and then it will start with the new block. Here you can clearly see that the async task is not performed in the Main thread whereas the sync task will run your code in the main thread, which affects the performance of your app.

Concurrent Queue asynchronous task

Here, I have changed the serial queue to Concurrent Queue by adding concurrent attributes.

Now here in the concurrent queue, the task will start executing all at a time. So here is the result of the concurrent execution.

Concurrent Queue synchronous task

Now herein, concurrent queue sync task.

The task will be performed in the main thread. In spite of being concurrent execution the task might be executed in order this is because of sync. This sync makes the task execute in an orderly manner that too in the main thread. So moreover it's like an Async task with Serial Queue If I'm wrong open to drop it on message.

DispatchWorkItem

DispatchWorkItem basically denotes the work you are going to provide to the dispatch queue. The ultimate use of this comes when you want to delay or cancel some work item.

Just for the understanding, I have used the for loop, and here if the previous work assigned is not executed before the new loop runs it gets canceled using the .cancel() method.

Now, here I tried to print the All task done at the end of the for loop but I noticed that it got executed first because of that async for-loop. If you want all the for loop has to be executed first and then the print statement should come, then in that case DispatchGroup comes in the picture.

DispatchGroup

In DispatchGroup, you can assign a set of the work item and feed it to DisptachQueue. You can assign it to the same dispatch queue or a different dispatch queue. You can even wait till the async execution gets completed using the wait method or you can even notify once the group of tasks gets comepleted.

From the example, it's pretty clear, assigned the DispatchGroup to the queue, and using the wait method I have to hold the execution.

You can even use notify method to trigger once when the Async task gets completed.

If you found it meaningful, just hit on the 👏🏻 It will motivate me to write some more blogs 🚀.

--

--

Agnel Selvan
Agnel Selvan

Written by Agnel Selvan

A person who is learning Flutter Development, Shaders, OpenGL, and Augmented Reality.

No responses yet