Let say I have bgQueue
in scope of some class function e.g.:
- (void)synchronize() {
NSOperationQueue *bgOperation = [NSOperationQueue new];
[bgOperation addOperations:@[fetchDataOperation, saveDataOperation, completionOperation] waitUntilFinished:NO];
}
whether this operation queue will stay in memory until all operations which contains gets finish or will be removed from memory when the method scope will ends?
I'm wondering whether operations holds in memory queue which they belongs to?
I couldn't find a good answer in the docs, but I built a test case to figure it out. It appears that even without a reference to an NSOperationQueue
, the queue remains until all operations have finished.
Here's the sample code, I ran runLotsOfPrints()
from within an iOS app:
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")
}
}
The resulting log output was as follows:
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