Concurrency in Swift (Operations and Operation Queue Part 3)
  1. What is Operations and It’s Life states
  2. Create Block, NSInvocationOperation , and Custom Operations to run tasks async
  3. How to cancel operations
  4. What is Operation Queues
  5. How to add Operations In Operation Queues
  6. How to create dependencies between Operations
  7. Benefits of Operation Queues Over GCD
  8. Dispatch Group Implementation Using Operation Queues


Operations State

  • When it’s been instantiated, it will transition to the isReady state.
  • When we invoked the start method, it will transition to the isExecuting state.
  • When the task finished , it moves to isFinished
  • When task is in progress and you call cancel , then it will transition to the isCancelled state before moving onto the isFinished state

1. BlockOperation (Concrete Class)

Figure 1
Figure 1.0.1
Figure 1.0.2
Figure 1.0.3

Run Block Operation Concurrently

Figure 1.1

2. NSInvocationOperation (Concrete Class)

3. Custom Operations

Figure 2
  1. Created subclass MyConcurrentQueue . Typo: The name should be MyConcurrentOperations
  2. Calling start() method will call main() method on background thread
  3. On main method we defined our task and one thing to note we cater cancel case as well
  4. On calling cancel on custom operation will transition to the isCancelled state and break the loop and as shown in Figure 3 it will print only 39487 items
Figure 3

Operation Queues

  1. Operations Queues are Cocoa’s high-level abstraction on GCD
  2. Using Operation Queues you will see the real power of operations , instead of the starting the operation yourself , you give it to the operation queue it then handle the scheduling and execution.
  3. Operation Queues are an object-oriented way to encapsulate work that you want to perform asynchronously.
  4. You add operations (tasks/work) on operation queue and we discussed how we can create operations by using two methods.

Add Operations

Figure 4
Figure 5
Figure 6

Operations Dependencies

Figure 7

Dispatch Group Implementation Using Operations Queue

  1. Created a operation queue
  2. Created three block operations that will perform tasks
  3. Created a completion block operation (blockOperations4) which will trigger when all three tasks will finished
  4. Made blockOperations4 dependent on blockOperations1, blockOperations2 and blockOperations3 which means blockOperations4 will execute when all three tasks will finished
  5. waitUntilFinished → Blocks execution of the current thread until the operation object finishes its task since we don’t want to block the current thread which is main we assign it with false
  6. Run this code and “All Operation is Completed” will print when task1, task2 and task3 will finished
Figure 8
Figure 9
Figure 10

Benefits of Operation Queues Over GCD

  1. The Operation API provides support for dependencies. You can create complex dependencies between tasks very easily though in GCD you can achieve it but you have to do a lot of work.
  2. The NSOperation and NSOperationQueue classes have a number of properties that can be observed, using KVO (Key Value Observing). This is another important benefit if you want to monitor the state of an operation or operation queue.
  3. Operations can be paused, resumed, and cancelled. Once you dispatch a task using Grand Central Dispatch, you no longer have control or insight into the execution of that task. The NSOperation API is more flexible in that respect, giving the developer control over the operation’s life cycle
  4. The NSOperationQueue also adds a number of benefits to the mix. For example, you can specify the maximum number of queued operations that can run simultaneously. This makes it easy to control how many operations run at the same time or to create a serial operation queue.


Useful Links



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