mrosales
mrosales

Reputation: 1568

Are NSOperationQueues retained automatically while running?

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?

Upvotes: 4

Views: 547

Answers (1)

matt
matt

Reputation: 535566

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.

Upvotes: 3

Related Questions