Concurrency In iOS — The Beginning

Diayan Siat
4 min readFeb 28, 2023

--

Photo by Felix Rottmann on Unsplash

In one of my previous articles, I discussed and showed how to make multiple API calls using Dispatch Groups which generated a lot of interest among my audience. I then made a promise to write a series of articles/tutorials on Concurrency in iOS to cover the entire topic. You can find the initial article from the link below:

https://medium.com/@diayansiat/make-multiple-api-calls-using-dispatch-group-f4e2f9f98aad

In this article, and several others to come we will seek to understand Concurrency, the need for concurrency, and how to achieve concurrency. Along the way I will compare and contrast Concurrency with topics like Parallelism. We will explore the Grand Central Dispatch (GCD), Operations and OperationQueues and Swift Concurrency. Without saying too much lets dive right in.

So What is Concurrency?

Sometimes, understanding a technical concept starts from knowing the meaning of the word used to describe it (remember it’s people like you who name such concepts using English words). That said, here is the Oxford dictionary’s first definition of the word Concurrency:

the fact of two or more events or circumstances happening or existing at the same time.

Synonyms: simultaneousness, coexistence

If you should take anything from this definition, it is simply that two events can occur at the same time. For example practicing an instrument (example piano) whiles reading sheet music, those two events could be said to occur concurrently. Or working on a project with a colleague, you can both write code at the same time and even push it to a shared repository at the same time. This could also be said to be concurrent events.

Now lets bring it home, computers are capable of performing multiple tasks at the same time (way better than us) and when this occurs during program execution, it is referred to as concurrency.

Consider a scenario where you use your phone to stream music whiles browsing your favourite social media, this is a classic example of concurrency in action. Here, your phone is juggling two activities, playing music in the background whiles you’re freely scrolling your feed or even interacting with other apps. And all of these without you losing your mind over poor performance.

Why should you care about Concurrency?

Well, it turns out users are not merciful when your app freezes their phone screens or even takes to long to perform certain actions. You risk getting your app uninstalled if it does not perform efficiently. Remember, there may probably be a thousand other apps on the Appstore doing the same thing as your app. And oh don’t ever assume they don’t notice these things, have you read app reviews :]?

Designing your application with Concurrency in mind has many benefits. It allows you to take advantage of the modern Computer hardware systems such as the multi-core processors. Some observable issues that may be fixed as a result of implementing concurrency include: improved UI responsiveness and eliminating UI freezing, improved user experience, for example allowing a user to download an image in the background whiles using the phone to perform other operations. In fact it improves the overall performance of your app.

If that’s the case, how do you use Concurrency in your app?

That’s the subject of this series. Generally speaking, you will learn how to write concurrent code to perform multiple tasks in your app while maintaining a great user experience.

Apple provides three modern ways of dealing with concurrency, Grand Central Dispatch (GCD), Operations and OperationQueues, and Swift Concurrency. In the next part of this series, we will explore GCD, learn how to use it to perform various kinds of concurrency issues like running background tasks.

Then we will explore Operations and OperationQueues which is built on top of GCD. We will learn the advantages it provides over GCD in performing more complex concurrency issues.

Finally we will look at Swift Concurrency which is Swift’s built in support for writing concurrent code. Swift concurrency is the newest concurrency tool provided by Apple to enable developers write more efficient code.

What’s Next?

This article is meant to give a brief introduction of what is to come. It is very essential to have a good understanding of the fundamentals. In the next several article, we will explore each of these topics in details one at a time.

If you’re interested in this topic, follow me to get the rest of the series as I release them. And leave a comment if you have them.

https://developer.apple.com/documentation/DISPATCH

https://developer.apple.com/documentation/foundation/operationqueue

https://developer.apple.com/documentation/foundation/operation

--

--

Diayan Siat

Android | iOS Engineer. I write about Mobile Engineering, Life Experience and Personal Development.