4

This is probably an easy one, but I did a couple quick searches and couldn't find the answer. Do I have to retain an NSOperationQueue (by using property etc) to avoid having it be released after a method finishes execution. For example:

- (void)doOperation:(NSOperation *)someOperation {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]
    [queue addOperation:someOperation];
}

I know that the operation will be retained by the queue while it is executing, but will the queue get released by arc as it has no explicit references outside the scope of this method?

mrosales
  • 1,543
  • 11
  • 18
  • Duplicate of http://stackoverflow.com/questions/2500933/is-it-dangerous-to-set-off-an-autoreleased-nsoperationqueue? – Martin R Jun 25 '14 at 17:58
  • It's similar, but that says that this is just undocumented behavior and I was curious about what would actually happen. I guess I will just retain the queue while it is running just to be safe. – mrosales Jun 25 '14 at 18:14
  • My experiments also show that the queue is retained. But as long as it is not documented one should probably not rely on it. – Martin R Jun 25 '14 at 18:16
  • What about declaring the NSOperationQueue as a weak property and overriding the getter for lazy instantiation? This way you would you wouldn't have to continually alloc/init a new queue, you could use the same property throughout your class and the memory management of the queue would be pretty efficient too. – Sadiq Jaffer Jun 27 '14 at 09:36
  • @sadiqJaffer, The problem is not with having a retained queue around and not using it, it is when a queue gets released by the system when it still has operations in it. – mrosales Jun 27 '14 at 16:54
  • 1
    @MartinR This may have happened since that comment, but the documentation (now) actually says "Operation queues retain operations until they're finished, _and queues themselves are retained until all operations are finished_." [Emphasis mine.] – matt May 03 '19 at 23:23
  • @matt: You are right. Out of curiosity I consulted the “way back machine”: That note was added to the documentation in December 2018 :) – Martin R May 04 '19 at 01:08

1 Answers1

3

The documentation says:

Operation queues retain operations until they're finished, and queues themselves are retained until all operations are finished.

Thus, the common pattern

[[[NSOperationQueue alloc] init] addOperation:op];

or, in Swift

OperationQueue().addOperation(op)

is safe.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Even though it works, isn't it a bad idea to do this? You won't be able to cancel the operations (e.g. if the app is going to the background) and it's *kind of* a memory leak if any of the operations block and can't be removed from the queue. Also, the operations themselves might have a reference back to a view controller which then wouldn't be released. – nevan king May 11 '19 at 12:08
  • Sure but how is any of that different from the common pattern of just jumping on a background DispatchQueue with GCD async? – matt May 11 '19 at 12:17
  • I don't know GCD well enough to compare but my first feeling when I saw this technique was that it would be prone to bugs – nevan king May 11 '19 at 15:10
  • Sure, I don’t advise it. But that wasn’t the question. – matt May 11 '19 at 16:02