Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it dangerous to set off an autoreleased NSOperationQueue?

I have a task that takes a rather long time and should run in the background. According to the documentation, this can be done using an NSOperationQueue. However, I do not want to keep a class-global copy of the NSOperationQueue since I really only use it for that one task. Hence, I just set it to autorelease and hope that it won't get released before the task is done. It works.
like this:

NSInvocationOperation *theTask = [NSInvocationOperation alloc];
theTask = [theTask initWithTarget:self
                         selector:@selector(doTask:)
                           object:nil];
NSOperationQueue *operationQueue = [[NSOperationQueue new] autorelease];
[operationQueue addOperation:theTask];
[theTask release];

I am kind of worried, though. Is this guaranteed to work? Or might operationQueue get deallocated at some point and take theTask with it?

like image 724
bastibe Avatar asked Mar 23 '10 15:03

bastibe


People also ask

What happens when an operation is queued but not yet executed?

For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished. Canceling an operation causes the operation to ignore any dependencies it may have.

Is the nsoperationqueue class KVC compliant?

The NSOperationQueue class is key-value coding (KVC) and key-value observing (KVO) compliant. You can observe these properties to control other parts of your application.

Can I use a single nsoperationqueue object from multiple threads?

For more information about KVO and how to attach observers to an object, see the Key-Value Observing Programming Guide. You can safely use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object.


3 Answers

There's nothing in the documentation to say what happens when the NSOperationQueue is released. It would be safest to assume there's no guarantee that theTask will get executed.

like image 52
Nick Moore Avatar answered Oct 05 '22 11:10

Nick Moore


I would have guessed that an NSOperationQueue releases its tasks when it's released, but I've noticed that the tasks do complete and dealloc even if I release the queue immediately after adding the task. That said, I don't think I'd rely on that behavior - there's more to gain by storing the NSOperationQueue in an instance variable (and releasing it in dealloc). An instance variable will give you a way to call other methods on the queue (cancelAllOperations, setSuspended, etc).

like image 28
dstnbrkr Avatar answered Oct 05 '22 12:10

dstnbrkr


Can't you use the [NSOperation mainQueue] object so that you don't need to worry about autoreleasing it? If you only need to add one task that seems to make the most sense to me.

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/doc/uid/TP40004592-RH2-SW21

like image 33
Steven Behnke Avatar answered Oct 05 '22 11:10

Steven Behnke