Mobile App Development Blog

Blog Home

iOS Development Guide: Concurrency with NSOperation and NSBlockOperation

July 20, 2016

ios-concurrency-

Multi-tasking is a skill. Not many of us are pro at it. Similarly while developing iOS applications creating multiple actions can be challenging. So if you want concurrent actions to be performed, rules are inevitable. Concurrency is processing more than one task at a time with specified rules that makes it easier to handle. Working on different threads at the same time, accounts to concurrency.

There are various ways to achieve concurrency; a popular one is through iOS SDK, developed by Apple Inc. But I have chosen NSBlockOperation and NSOperationQueue. Basically, when you are doing some time-consuming tasks, if you do not want to involve main thread, you can use NSOperationQueue and NSBlockOperationQueue. These are responsible for UI responsiveness. So if you are going to do a heavy operation on Main Thread, which is taking time, user definitely will get stuck in the UI.

The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. This is very simple concept, here’s an explanation with an example:

Example:

Suppose I have a big range of numbers and I want to sum up all the numbers. Let’s say 1 to 500000000.

Doing sum on Main Thread might block the UI until it’s done and the app will not give any response.


override func viewDidLoad() {

super.viewDidLoad()

calculate()

// Do any additional setup after loading the view, typically from a nib.

 

}

 

 

private func calculate(){

 

var sum:Double = 0

for i in 0…5000000000

{

sum = sum + Double(i);

NSLog(“%d”, i);

}

}


While executing the calculate () it takes a long time. At this point of time UI elements (UIButton,UISlider) might not respond properly.

 

So if you want to do this operation concurrently without affecting the Main Thread, then we can use NSBlockOperation and NSOperationQueue to finish this job without blocking UI element.

 

Example 2:


override func viewDidLoad() {

super.viewDidLoad()

calculate()

// Do any additional setup after loading the view, typically from a nib.

}

 

private func calculate(){

 

let queue = NSOperationQueue ();

 

let blockOperation = NSBlockOperation {

 

var sum:Double = 0

for i in 0…5000000000

{

sum = sum + Double(i);

NSLog(“%d”, i);

}

 

NSOperationQueue.mainQueue().addOperationWithBlock({ () ->Void in

 

 

self.label.text = “\(sum)” // updating sum to UI

})

}

queue.addOperation(blockOperation);

 

}


I could not wait for the result. Just tested it while calculating and UI element was accessible. And that’s all what we wanted to achieve.

Hope you guys like it.

 

About the author

Sanoj Kumar Kashyap is a senior iOS developer who believes that developing app for any device is always fun. He has mostly worked for iPhone and is eager to jump to other device platforms. He is also an ardent fan of photography, gaming and cooking.

Posted Under: