01 logo

Concurrency in iOS

Concurrency with DispatchQueues

By ConficlePublished about a year ago 5 min read

Hello all, we are back with our next article. In case you would like to refer our previous articles, you can find them here.

Introduction

Concurrency is not only a common, but a required feature of software engineering. It is part of basic syllabus taught to college grads, and a required skill expected in software developers of all the levels.

Let’s try to brainstorm this concept through this article. This article tries to explain

  1. Common definitions and concepts related to concurrency.
  2. How concurrency is implemented in iOS using dispatch queues.

So, let’s dive in.

Common definitions & concepts

Process

By definition. An executing instance of a program is called a process. So, when an application is started or booted up, its running instance is called process.

A process is always stored in main memory also called primary memory, which is commonly referred as random-access memory or just RAM.

A process disappears if machine is shut down or rebooted.

A very common example can be, when a project is opened in Xcode, a single instance of Xcode application starts executing in RAM. This running instance of Xcode application is called a process.

Thread

Thread is a subset of process. When process starts, it is started with a single thread (also called primary thread), but it can create additional threads also.

So, a thread is a part of process, that runs inside the context of process and shares the same resources allotted to process by kernel.

Concurrency

Concurrency is a general concept, where more than one tasks are executed simultaneously, but not necessary in parallel.

Concurrency comes into picture when more than one task, that are independent of each other are required to run simultaneously, either on single thread or multi-thread system.

Tasks that run concurrently are NOT always running in parallel.

A real-world example can be, let say you want to eat and sing at same time. But for both tasks you have single resource or thread (your mouth). But both tasks are independent of each other, so both can be run simultaneously. Like you can start by eating, but after one bite you may start singing, now before the song is finished you take another bite.

So, it’s up to you to decide how can you finish both the tasks (singing and eating).

Concurrency with single thread is achieved using time-slicing or interleaved processing.

Parallelism

Parallelism is a form of concurrency, in which multiple tasks are executed using multiple threads and task run in parallel.

Tasks that run in parallel are also considered concurrent tasks.

A real-world example can be, let say you want to bathe and sing. Both tasks do not share a single resource or thread. You can bathe while singing and execute both the tasks in parallel.

Asynchronous task

Asynchronous task is same as concurrent task, but with a promise of calling back when task is finished.

Asynchronous task like concurrent task can also be executed on single or multiple threads.

Most common example of asynchronous tasks include calling rest apis, loading a file, processing a large image etc.

Concurrency in iOS using DispatchQueue

There are multiple methods in iOS to implement concurrency.

DispatchQueues are one of them. DispatchQueues are low level implementation of NSOperationQueue, which can also be used to implement concurrency.

DispatchQueues and NSOperationQueue take away the overhead of creating threads from the developer and hiding thread creation and management under hood. When using DispatchQueues or NSOperationQueue, developer just need to specify which tasks or operation needs to be run synchronously or asynchronously in a serialized or concurrent sequence.

Developer can also define priority of tasks to control the order of execution.

Types of dispatch queues

There are 4 types of DispatchQueues supported by iOS.

Main Queue

Main queue can be accessed using DispatchQueue.main. All the tasks or operation dispatched using main queue will run on main thread. Tasks can be dispatched on main queue using below methods.

  1. DispatchQueue.main.sync() - All the tasks dispatched using this method will run on main thread and in a serial order. Do not call DispatchQueue.main.sync() method from main thread, as it will result in deadlock and application will crash.
  2. DispatchQueue.main.async() - All the tasks dispatched using this method will run on main thread and in a serial order. This method can be used to update UI when receiving callback from background thread. Most common example is to update UI after receiving response from rest api callback.

Example

As shown in above screenshot. 3 tasks are added to Dispatch.main.async() method and all tasks are executed on main thread, in a serial order.

Serial Queue

Serial queue can be accesses by creating instance of DispatchQueue as below

let customQueue = DispatchQueue(label: "Custom queue")

Tasks can be dispatched on serial queue using below methods.

  1. customQueue.sync() - All the tasks dispatched using this method will run on main thread and in a serial order.
  2. customQueue.async() - This method will create one new thread for all tasks dispatched using this method. And all the tasks will execute in a serial order on that new thread.

Example

As shown in above screen shot.

Operation1 is executed on .sync() method. Hence it is executed on main thread.

Operation2 & Operation3 are executed on .async() method. Hence these are executed on different thread in a serial order.

Concurrent Queue

Concurrent queue can be accesses by creating instance of DispatchQueue as below

let customQueue = DispatchQueue(label: "Custom queue" , attributes: .concurrent)

Tasks can be dispatched on concurrent queue using below methods.

  1. customQueue.sync() - All the tasks dispatched using this method will run on main thread and in a serial order.
  2. customQueue.async() - This method will create new thread for each task dispatched using this method. And all the tasks will execute in a concurrent order on respective threads.
  3. Example

As shown in above screen shot.

Operation1 is executed on .sync() method. Hence it is executed on main thread.

Operation2 & Operation3 are executed on .async() method. These tasks are executed concurrently on 2 different threads.

Operation2 is executed on thread with memory address 0x600001748c00 and number 3

Operation3 is executed thread with memory address 0x600001754440 and number 2

Global queue

Global queue can be accesses by using DispatchQueue.global(). Global queue behaves in same manner as concurrent queue.

Tasks can be dispatched on concurrent queue using below methods.

  1. DispatchQueue.global().sync() - All the tasks dispatched using this method will run on main thread and in a serial order.
  2. DispatchQueue.global().async() - This method will create new thread for each task dispatched using this method. And all the tasks will execute in a concurrent order on respective threads.

Example

    As shown in above screen shot.
    Operation1 is executed on .sync() method. Hence it is executed on main thread.
    Operation2 & operation3 are executed on .async() method. These tasks are executed concurrently on 2 different threads.
    Operation2 is executed on thread with memory address 0x600001744840 and number 3
    Operation3 is executed on thread with memory address 0x600001754240 and number 4

Summary

The above queues can be summarized in a table below.

Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

appshow totech newsmobile

About the Creator

Conficle

A passionate iOS developer with more than decade of experience in creating enterprise applications for Apple devices. With a solid foundation in Swift, Objective-C, I thrive on transforming complex ideas into elegant, functional apps.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.