Search code examples
iosobjective-cnsoperationnsoperationqueue

Deallocate NSOperationQueue as soon as all operations in queue are finished


I want to deallocate NSOperationQueue when all the operations in progress are finished executing. So far, I have coded below but as far as I know waitUntilAllOperationsAreFinished is async call and could not hold from my operationQueue getting nil.

- (void)deallocOperationQueue
{
    [operationQueue waitUntilAllOperationsAreFinished];
    operationQueue = nil;
}

Solution

  • Quoting Avi

    You don't need to wait for all operations to finish. Just set operationQueue to nil when you're done with it. If the queue still has operations, nothing happens to them; they will still complete.

    - (void)deallocOperationQueue
    {
        operationQueue = nil;
    }
    

    I have tested the code and confirm that stated behaviour does happen.