Multithreading in iOS – Part 4

Operation Queues

Apart from GCD, Operation Queues can also be used to solve the multithreading scenarios in swift. 

Most of the concurrency problems can be solved using GCD. But there are times when we need some control over execution of the tasks. This is when Operation Queues can be used to solve those issues.

Operation Queues provides more control over the threads. It is a wrapper over GCD which provides additional features like dependencies between the tasks, pause, resume and cancel the tasks, maximumConcurrentOperation, etc.

An Operation is an abstract class and we cannot create an instance of Operation class directly.
Apple provides BlockOperation class which is a subclass of the Operation class to manage the concurrent execution of one or more blocks.

Executing tasks using Block Operation:

A block operation can be created like below:

let operation = BlockOperation()

Let us first have a Network Manager class where we call multiple tasks like below:

1. Network Manager

In the above Network Manager class, we have three functions.
To simulate the function call delay, we have an array [1,2,3,4]. In each task, we get the random element in this array and wait here in this method for this many seconds by using sleep method before calling the completion.

Now we can call these tasks in the View Controller using block operation as shown below:

2. Block Operation

In the above method, we have created a block operation instance. We then add the tasks to the block operation and start the operation using operation.start() method.

By default tasks run asynchronously in block operation. The output is as shown below:

3. Block Operation Output

In the above results, we could see that the task two has executed first and then task 1 has started. 

Using Operation Queue:

Tasks can be executed asynchronously using Block Operation and start the tasks using start() function. 

Apple suggests to create an Operation Queue and add tasks to the operation queue as system will take care of execution of the tasks. 

Another advantage of using operation queue is that we can add multiple operations to the operation queue like below:

4. Operation Queue

In the above example, we have created two block operation instances and an Operation Queue.

Added the tasks to the operations and added the operation to the operation queue. Operation queue will take care of executing the operations in an asynchronous manner. The output will look like below:

5. Operation Queue output

In the above example, we could see that the operations have executed asynchronously. 

Operation queues has many advantages over GCD like:

  • Add dependencies between the tasks
  • Pause, resume and cancel the tasks
  • control maximumConcurrentExecution of tasks
  • KVO complaint to check the status of the tasks and etc

Operation Queue by itself is a very big topic to cover the different scenarios. For more reference on the operation queue, please have a look at the Apple Documentation: https://developer.apple.com/documentation/foundation/operationqueue

If you have missed my previous blogs on multithreading, please have a look at those below:
1. Introduction to multithreading 
2. Grand Central Dispatch
3. Dispatch Group

Leave a Comment

Your email address will not be published. Required fields are marked *