NSOperation – Basics

NSOperation and NSOperationQueue are built on top of GCD and provide additional features built-in that are very hard to achieve using just GCD. This post will provide basic introduction of NSOperation and how to create a simple custom NSOperation and execute it. In my next post on this series, I will delve into NSOperationQueue and how to use your custom NSOperation in conjunction with NSOperationQueue to satisfy your different scheduling requirements.


NSOperation is an abstract class that provides a way to consolidate the data required and the code associated with a task. In order to create a custom operation, we should derive from the NSOperation class and add the data and code associated with our custom operation. Alternatively we can use one of the 2 Apple-provided subclasses of NSOperation: NSBlockOperation and NSInvocationOperation. I will not go into details of these specific subclasses of NSOperation here.

Features of NSOperation

NSOperation, in spite of being an abstract class, provides wealth of features associated with executing a task. The following are the two most important features which , though not impossible, are very messy to implement with GCD:

  • Tracking state of an task 
    •  The states that a task wrapped in NSOperation can take are “Ready”, “Executing” and “Finished”
  • Ability to cancel an operation.
    • NSOperation provides the ability to cancel an operation by cancel() method.
    • At the core, the  cancel() method just sets a boolean cancelled = true. For any of the custom subclasses of NSOperation, the onus is on us to ensure that we account for the value of the cancelled boolean and gracefully exit our task.
    • Will go into more details further in the blog

Below I will list the steps that go into encompassing a task into an NSOperation. For simplicity, the example I am going to describe below will execute synchronously ( i.e When the code execution finishes the task finishes (or) task does not launch any asynchronous code as part of its logic)

custom NSOperation Example 

Let’s say our gazelle(or deer) is in the forest grazing. A single unit of task that the gazelle needs to perform is checkForPredators(). In short, the gazelle looks up, checks its surrounding and will be done with the task.

In this scenario,

  • the data associated with the task would be the gazelle itself and
  • the logic associated with the task would be the call to “checkForPredators()”

In this most straightforward scenario, the new task only needs to override the main() method of the NSOperation abstract class. Below are the 3 things to remember when overriding main():

  • Ensure you are setting self.executing = true before starting task logic in main()
  • Ensure you are setting self.executing = false after task logic execution completes
  • Check if the operation has been cancelled and exit gracefully

Below is how the code will look for this new Custom NSOperation

The simplest way to start the execution of a custom subclass of NSOperation is to call the start() method. When we look at the open source code for NSOperation ( in Swift Yay!!) here, we notice that the start() method is as below

Hence, when we call GazelleCheckPredatorsOperation.start(), our custom implementation of the main() is called and then finish(), which cleans up the state of the Operation as soon as the main() function call returns.

CONGRATULATIONS !!!! you now have successfully created the basic NSOperation and started it to perform a pre-defined task!!!

NSOperation – Basics

Leave a Reply

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