In Swift: NSBlockOperation and NSOperationQueue Sample


Let's first understand Concurrency  which I am going to explain below: 

Concurrency: It tells running several tasks in same time. So do some work on Main thread and on other thread at the same time, will come under concurrency.

The iOS SDK and other way are there to achieve the same thing, but I have chosen  NSBlockOperation and NSOperationQueue.
The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. This is a very simple concept which I will explain in a very simple example. 

There can be other use as well but I understood and used it in one scenario. 

So, when you are doing some tasks, that tasks are taking time so you can use NSOperationQueue and NSBlockOperation queue if you don not want to involve Main Thread, as it is responsible for UI responsiveness. So if you are going to do a heavy operation  on Main Thread, which is taking time, user defiantly will stick in the UI

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 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 to calculate it. At this point of time UI element (UIButton, UISlider) object 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:


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);
    }

The result might not come (Could not wait for the result), Just tested that while calculating UI element were accessible. That's all what we wanted to achieved.

So the guy's  here is simple example of  NSOperationQueue and NSBlockOperation. Hope you will like it.

Please, let me know your suggestion and queries.

Stay tuned for next topic








Comments

Popular posts from this blog

Copy Vs Mutable Copy in iOS

How to Disable iOS/iPad App availability on Mac Store, Silicon?

Closures in Swift - Part 1