NSOperation on iPhone

I was looking for some specific scenarios when NSOperation on iPhone is the perfect tool to use in an application. As far as I understand, this is a wrapper for writing your own threaded code. I haven’t seen a single demo application at Apple, and I’m wondering if I’m missing a great tool instead of using NSThread .

The ideal solution here is to describe a use case for NSOperation and how you will use it to solve your problems.

+41
multithreading objective-c iphone cocoa-touch nsoperation
May 6 '09 at 3:36
source share
7 answers

Cocoa My girlfriend has a good tutorial about using NSOperation and NSOperationQueue . The tutorial uses NSOperation to load multiple web pages simultaneously in separate streams.

Also see this article from Mac Research.

+43
May 06 '09 at 18:26
source share

The way I use it in my iPhone apps is to basically create an NSOperationQueue member in my application’s dellet and make it available through the property. Then every time I need to run something in the background, for example. download some XML. I will just create an NSInvocationOperation and send it to the queue.

 NSInvocationOperation *operationToPerform = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateXML) object:nil]; [[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:operationToPerform]; [op release]; 
+33
May 7, '09 at 8:57
source share

In a word: NSOperationQueue

NSOperationQueue is thread safe (you can add operations from different threads to it without the need for locks) and allows you to combine NSOp objects together.

My Flickr iPhone app, Reflections, uses NSOperation and NSOperationQueue to control the loading of images and XML .

Warning. Make sure you read, reread, and understand what documents mean when they talk about "concurrency."

+10
May 7 '09 at
source share

You should also check this URL: http://developer.apple.com/cocoa/managingconcurrency.html

All of these answers are great, but make sure you read the article above and make liberal use of this line in your code:

 if ( self.isCancelled ) return; 

This line was not used in samples provided by Coca - my girlfriend, and only after I received the crash logs did I realize that this was a problem / concept.

+6
Jun 09 2018-11-11T00:
source share

Here is just a very simple implementation, but you have time to read the tutorials to fully understand everything:

 NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodToCall) object:objectToPassToMethod]; [queue addOperation:operation]; 
+5
Mar 27 '13 at 19:54
source share

I use it for asynchronous processing. This is the best way to retrieve data from web services or to coordinate actions that take a significant amount of time to start. Since they are thread safe, asynchronous (do not bind the main thread), and they support dependencies, they are a really great tool for your toolbox.

Dependencies allow you to make several separate operations and make sure that the execution and successful execution or error in a certain order. This is really great when you need to synchronize a bunch of data, but you need the parent objects to synchronize before the children synchronize.

+1
May 21 '13 at 22:04
source share

Sample you can try using Swift

 let operation : NSOperation = NSOperation() operation.completionBlock = { println("Completed") } let operationQueue = NSOperationQueue.mainQueue() operationQueue.addOperation(operation) 
+1
Jul 27 '14 at 19:02
source share



All Articles