NSOperationQueue 将在内存中保留多长时间?

How long NSOperationQueue will stay in memory?

假设我在某些 class 函数的范围内有 bgQueue,例如:

- (void)synchronize() {
    NSOperationQueue *bgOperation = [NSOperationQueue new];
   [bgOperation addOperations:@[fetchDataOperation, saveDataOperation, completionOperation] waitUntilFinished:NO];
}

此操作队列是会一直保留在内存中直到包含的所有操作都完成,还是会在方法作用域结束时从内存中删除?

我想知道操作是否保存在它们所属的内存队列中?

我在文档中找不到好的答案,但我构建了一个测试用例来弄明白。似乎即使没有对 NSOperationQueue 的引用,队列仍然存在,直到所有操作完成。

这是示例代码,我 运行 runLotsOfPrints() 来自 iOS 应用程序:

func runLotsOfPrints() {

    var loopCount = 0
    let maxPrintCount = 10000
    var operations: [NSOperation] = []
    while loopCount < maxPrintCount {

        let newOperation = localOperation()
        newOperation.localNumber = loopCount
        operations.append(newOperation)
        loopCount += 1
    }

    let queue = localOperationQueue()
    queue.addOperations(operations, waitUntilFinished: false)
}

class localOperation: NSOperation {

    var localNumber: Int = 0

    override func main() {
        super.main()
        NSLog("localN - \(self.localNumber)")
    }
}

class localOperationQueue: NSOperationQueue {
    deinit {
        print("deinit - QUEUE")
    }
}

结果日志输出如下:

2016-02-23 10:50:51.388 RobotsAreMyFriends[12363:35784085] localN - 1
2016-02-23 10:50:51.388 RobotsAreMyFriends[12363:35784114] localN - 2
...
2016-02-23 10:50:51.389 RobotsAreMyFriends[12363:35784131] localN - 9998
2016-02-23 10:50:51.388 RobotsAreMyFriends[12363:35784108] localN - 9997
2016-02-23 10:50:51.389 RobotsAreMyFriends[12363:35784113] localN - 9999
2016-02-23 10:50:51.476 RobotsAreMyFriends[12363:35784108] deinit - QUEUE