Using NSOperation and NSOperationQueues


 

As iOS devices have less resources in terms of memory, all long running and secondary task should not be run in the main thread. Running any long process in main thread causes it to block and app becomes unresponsive and thats uncool from UI perspective.

A solution is to use dispatch_async but that doesn’t gives control on the thread. We can’t schedule thread and once its launched it can’t be stopped or paused.

In such cases NSOperations comes into play. It’s a high level wrapper to threads in iOS and you can schedule them, control number of threads launched at a time, number of process to run at a time, cancel a thread, reschedule a thread, pause a thread, make a thread dependant on other thread to ensure the correct flow. This cleans the app structure and gives us a greater control over threads and also makes app less buggy.

A classic data structure to use to handle NSOpeations and NSOperationQueue in a clean way is described in following link:

http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

Posted in All Tutorials, iOS Tutorials | Tagged | Leave a comment

Leave a Reply

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

Powered By: wpnova.com