NSOperation on the iPhone

I've been looking for some concrete scenarios for when NSOperation on the iPhone is an ideal tool to use in an application. To my understanding, this is a wrapper around writing your own threaded code. I haven't seen any Apple demo apps using it, and I'm wondering if I'm missing out on a great tool instead of using NSThread.

The ideal solution here would be to describe a use-case scenario for NSOperation and how you would use it to solve your problem(s).

Answers


Cocoa Is My Girlfriend has a good tutorial on the use of NSOperation and NSOperationQueue. The tutorial makes use of NSOperation to download several webpages simultaneously in separate threads.

Also, see this article from Mac Research.


The way I use it in my iPhone apps is to basically create an NSOperationQueue member in my application delegate and make it available through a property. Then every time I need to run something in the background, e.g. download some XML I'll just create an NSInvocationOperation and send it to the queque.

NSInvocationOperation *operationToPerform = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateXML) object:nil];
[[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:operationToPerform];
[op release];

In a word: NSOperationQueue

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

My Flickr iPhone app, Reflections, uses NSOperation and NSOperationQueue extensively to manage downloading images and XML.

Caveat: Make sure you read, re-read, and understand what the docs mean when they talk about 'concurrency'.


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

All these above 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;

That line wasn't used in the samples provided by Coca is my Girlfriend, and it wasn't until I got crash logs in from the field that I realized this was an issue/concept.


Here is just a very simple implementation but take 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];

I use it for asynchronous processing. It is the best way to get data from web services or to coordinate actions that take significant time to run. Because they are thread safe, asynchronous (doesn't tie up the main thread) and they support dependencies, they are a really great tool for your toolset.

Dependencies allow you to make several separate operations and make sure the execute and succeed or error out in a certain order. This is really great when you need to synchronize a bunch of data but you need parent objects to sync before syncing child objects.


A sample that you can try using Swift

let operation : NSOperation = NSOperation()
operation.completionBlock = {
println("Completed")
}

let operationQueue = NSOperationQueue.mainQueue()
operationQueue.addOperation(operation)

Need Your Help

Can you autoplay HTML5 videos on the iPad?

jquery ios objective-c html5 cocoa-touch

The <video> tags autoplay="autoplay" attribute works fine in Safari.

Google OAUTH: The redirect URI in the request did not match a registered redirect URI

java youtube-api google-oauth youtube-data-api google-oauth-java-client

I am trying to make an upload to YouTube from my Java based web app, I spent a few days to understand what and where is the problem and I cannot get it, for now I am pulling my hair out off my head...