Multithreaded Programming in iOS

Learn which API to use for multithreaded programming in iOS.

Medi Assumani
4 min readSep 16, 2019

Speed, performance, and responsiveness become essential factors when working on enterprise-level applications. They also play a major role in the user experience, which in turn may yield positive or negative user retention outcomes. But how do we make our apps perform faster? Multithreading! This term refers to the ability of an operating system(iOS or macOS in our case) to use its many cores and threads to perform multiple tasks simultaneously. In this article, we will solely look into two main multithreading APIs provided by apple: GCD and Operation.

Disclaimer & Note

For this article, I am going to assume that the intended reader has a basic understanding of multithreading concepts such as process, thread, task, race condition, semaphores, and more. Let’s get into it!

Apple’s Multithreading APIs

Apple provides 2 main APIs for writing multithreaded code: Grand Central Dispatch(GCD) and Operation. Behind the scenes, they use a concept known as thread pool, meaning that those API manages a large number of threads and when a task is ready to be executed, it grabs one thread from the pool to take care of the task. Now let’s dig deeper into both of those APIs.

Grand Central Dispatch(GCD)

“Grand Central Dispatch is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems” _ Wikipedia

That’s a pretty intense definition right there, let’s break it down a bit. Remember when we talked about how threads are responsible for executing tasks, well iOS developers had to interact with the Thread class directly to set them up. In the release of iOS 4, Apple decided to build a wrapper that extracts all the low-level components of multithreaded systems from the developers into a sweet and powerful API, GCD. It uses a concept of Queue to organize the order at which tasks are submitted to threads for execution.

An easy and simple way to execute concurrent tasks using GCD.

Operation

Operation is the second API provided by Apple to write concurrent applications. It is to be noted that Operation is built on top of GCD and is recommended over GCD by apple due to its higher level of abstraction.

One thing to keep in mind is that the Operation class is abstract, meaning that you must create a custom class that inherits it and overrides the main function. Inside the main function, you can write the logic of the operation. Below is an example of a simple Image download operation:

class ImageDownloader: Operation {        // Designated initializer
init() {
// You can initialize some data here

}
// Override the main function of the Operation class
override func main() {
// Write the logic of the operation here
}
}

Compare & Contrast

GCD uses callbacks mechanism to group a block of code that needs to run asynchronously on a given thread using DispatchQueues. The system uses a mechanism to prioritize running tasks known as Quality Of Service(QoS)(e.g: user interactive, default, utility, etc…). One of the most common task in iOS that uses GCD is a simple network request to a remote server on a background thread that returns valid data which updates the UI on the main thread.

Example of how a simple networking layer works with threads.

On the other hand, Operation API gives more control to developers such as controlling the max number of concurrent tasks to run simultaneously, pass priority levels to each execution and control the flow of the operation(pause, resume, and cancel). Additionally, it allows you to bind two or more operations together using dependencies. Lastly, the Operation class has many properties that can be observed using KVO, which allows developers to monitor its state.

The OprationQueue class, same as DispatchQueue in GCD, controls one or many ops.

Conclusion

To conclude, choosing the right API to use comes down to the task you have at hand. Make sure to analyze and consider the tradeoffs between the two before choosing. I hope you found this article helpful in some way. If you did, don’t forget to leave some claps👏🏾 and feel free to comment if you have any question🚀

About the Author :

Medi is a Software Engineer specialized in Mobile iOS development. He enjoys writing about Swift, iOS, Productivity, and Tech in general .You can get in touch with him through his email, LinkedIn or Portfolio.

--

--

Medi Assumani

iOS Engineer @ Doordash. Writing about Mobile development and software engineering in general. Views and contents here are my own, of course.