tags:

views:

31

answers:

1

I have a piece of code which will run only once in the background when the app loads. I don't want to worry about memory management of the NSOperationQueue *, can I autorelease it?

 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(oneTimeTask) object:nil];
 [queue addOperation:op];
 [op release];
 [queue autorelease];

Thanks

+2  A: 

Short answer is no, if you want it to exist till your app exits.
If you auto-release it, the queue object will be released (and so deallocated) at the next cycle of the event loop, which you probably don't want...

Macmade
I don't need it to exist till the app ends. I want it cleaned up after the last `NSInvocationOperation` is completed.
ohho
Then use the `isFinished` method on the `NSOperation` objects, and explicitly release the queue when all the operations are finished. If you auto-release the queue, it may be deallocated when operations are still running, or waiting.
Macmade