Concurrency Programming with Operation Queues - Part 1

With the number of cores increasing in our phones, we the software developers need to start making use of them, and for that, we need to know what Concurrency Programming is and understand how to make use of it.

Concurrency in Programming simply means multiple tasks executing at the same time. iOS makes use of Concurrency extensively and requires us to do the same whenever needed, to maintain the quality and performance of our apps.

To make use of multiple cores, we need to create multiple threads, but the problem with threads is you cannot create threads more than the cores you have, and the number of cores depends on device-to-device. Handling threads is a challenging task, and any mistakes can lead to your application crashing or lagging, which is not our intent.

To solve this problem, iOS comes with a different approach known as ‘asynchronous design approach’, which shortly means, iOS provides us with some solutions, which does all the thread management code.

One way to do this is GCD, and I have a pack of articles already available for you on my website, starting with, How to use GCD in Swift?, Click Here to Read.

In this article, we will look into Operation Queues, how to use it, and explore more details about it.

Executing Operations with OperationQueue

Before we start exploring Operation Queues, you need to know what Operation is.

Operation is an abstract class, which also means it cannot be used directly. We can subclass Operation or use system-defined BlockOperation to perform the task. We can call the start() function on the operation to start executing the task, as shown below.

let operation = BlockOperation {
    print("An Operation")
}

operation.start()

Using start() to execute a task is not preferred, as we do not know if the operation is ready to execute. That is where OperationQueue comes in. As OperationQueue handles, all the Operations added in the queue based on their availability and priority. An example of OperationQueue with multiple Operations is shown below.

let operation = BlockOperation {
    print("An Operation")
}

let queue = OperationQueue()
queue.addOperation(operation)

queue.addOperation {
    print("Another Operation")
}

As shown in the above code, you can create an Operation directly in the block provided by addOperation function.

Operations is a great way of executing asynchronous functions with extra support like cancelling the operation or adding dependencies to it.

In my next article, we will be exploring OperationQueues in-depth and we’ll also look at how to cancel or add a dependency in Operation.

Click here to read, Concurrency Programming with Operation Queues - Part 2.

I hope you guys enjoyed reading my article. There’s a lot more to learn together, so subscribe to stay updated about my upcoming articles.

If you have any suggestions or questions, Feel free to connect me on Twitter or Reddit. 😉

Concurrency Programming with Operation Queues - Part 2

Generics Introduction in Swift