如果我有一个 View Controller ,它有一个 NSOperationQueue
作为实例变量,那么如果 View Controller 被卸载,队列中正在执行的任何操作会发生什么?
最佳答案
我原来的回答是个愚蠢的错误。我之前读错了这个确切的文档。真正的答案是没有定义会发生什么,或者至少没有记录。
正如我在 this answer 中提到的有几件事表明操作队列将被保留(比如 GCD 队列在执行 block 时被保留的事实)。
This question about autoreleasing an NSOperationQueue也说了同样的话:
There's nothing in the documentation to say what happens when the NSOperationQueue is released.
您当然可以解释“指定的操作保留在队列中,直到它完成执行”,因为队列不会消失,直到排队的操作完成执行,但我仍然不会指望它。
但是,如果您想确保它们不执行,您可以取消操作,方法是在卸载 View Controller 时告诉操作队列cancelAllOperations
,如果您想防止这种情况发生。请注意,操作不会自行停止。他们需要检查 if ([self isCancelled]) { ... }
并停在那里。
原答案:
所有操作都将完成执行,因为它们保留了它们的队列。来自 the documentation of addOperation:
Discussion
Once added, the specified operation remains in the queue until it finishes executing.
关于iphone - 使用 NSOperationQueue 作为 View Controller 实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064194/