Threads & Concurrency In Swift

Multiple Threading swift
Swift Threading

Main Thread & Background Thread:

Main thread is where user’s perform the interaction for example the button or scrolling of tableView. Where as the background thread is where you perform your heavy operations e.g API calls or downloading of an image.

If we call the background thread function on our main thread it will freeze the UI (user interface) & the user won’t be able to perform any interactions with the app that will make him think that the app is freezed.

Thread and Concurrency

The above gif is from an app called Blatini it is a classified app that helps you buy and sell businesses online kind of a classified but for businesses can be downloaded from here.

Look how first they shows the loading label and then the UI loads i.e the tableView cells. when the loading label appears they block the main thread and once the data is loaded they unblock the main thread. This is what happens when you do the API calls, image downloading or database related work on main thread. When the loading label is their you cannot interact with the app.

Apple has two API’s that helps us to handle Background-thread and Main-thread those API’s are GCD (Grand Central Dispatch) & NSOperationQueue.

What is a Queue?

Thread and Concurrency
Photo by John Cameron on Unsplash

The image above shows a queue in a super market where all the people are lined up for checkout. The queue follow’s a rule called fifo( first in first out).

The one who comes first in the queue will be the one who finishes the checkout first now what is the checkout procedure or the steps for checkout.

  • Put your items on the counter.
  • The cashier will scan your items
  • The cashier will tell you total amount.
  • You will pay the amount.
  • If there is a change e.g your bill was 450 and you paid him 500 so the cashier will give you change.
  • you will take the bill and pick up your items and leave.

Once the above steps are completed we can say your operation i.e checkout is completed and now the cashier will deal with the person who is after you.

The same thing happens in Swift Serial Queue operation.

Serial Queue Swift
Serial Queue in Swift

In the above Serial Queue the Task-A will complete 100% and only then Task-B will start and once its 100% done then only the last task Task-C will begin.

Concurrent Queue?

Concurrent means “happening in same time” or simultaneously. For example you are writing a code and listening to a music at the same time you never know if your code is going to finish before your music or your music will stop playing before you finish your code. Both of these tasks are being executed at same time but not sure which one will be finish before the other. This is how concurrent queue works.

When to use which queue?

Serial Queue:

  • Executes tasks one after the other.
  • When tasks order is important.

lets say I’ve an app where I want to fetch data from API and store it in my phone read from the local storeage and then display it to my user.

Serial Queue Swift
Serial Queue Swift

I cannot store without fetching, I cannot read without storing and I cannot display without reading. So each task depends on other & order of tasks is important. Thats why we will be using Serial Queue here.

Concurrent Queue:

  • All tasks executes at same time.
  • Not sure which task will complete first.
  • When order of tasks is not important.

I hope you all are familiar with the famous library called ‘SDWebImage’ this library helps you to download the images from url. Let’s say I’ve a tableView the entire data is loaded I’ve a place holder image for all the cells until their image from the server is not downloaded. I’ve 10 cells and they all will show their url images anytime regardless their order here we are using concurrent queue.

What is a Race Condition:

Race condition by defination is when multiple threads tries to “Write” or “Modify” a single shared resource at the same time. How ever if multiple threads “Read” a single file at same time a race condition won’t occur.

Lets say I’ve a bank account with balance of 2000$. I am sending a 1000$ to my brother where as at the same time my wife is withdrawing an amount of 1500$ from same account.

In this case my account is shared resource me and my wife is acting a threads. If the bank is using a concurrent queue and hasn’t handled these kinds of situations in their code a race condition will occur in this case.

I hope I was able to deliver a breif explanation and understanding of this topic. If you want me to keep writing please follow me this really motivates me to write more. If you have any special request do not hesitate to write me.




An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter sign in/up with Firebase Authentication

My Recipe For Learning

Complete Git and Github Guide

Software Testing & Testineering: It’s Not Just an Act, it’s a Habit

Yes, we know Git. What about Git Workflow?

Magento Theme Development Tutorial: A step-by-step guide for beginners

How to run C# file in MatLab via script?

Techies’ meetup groups in Amsterdam

Group of people brainstorming ideas illustrated.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Umair Ishrat Khan

Umair Ishrat Khan

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

More from Medium

iOS library support both Cocoapods and Swift Package Manager

iOS Application States

iOS Fundamentals — iOS Tour Part 4(Function and Closures)

Practical Reference Cycles in Swift