iphone - 使用 NSOperationQueue 作为 View Controller 实例变量

标签 iphone ios cocoa-touch nsoperation nsoperationqueue

如果我有一个 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/

相关文章:

iphone - 从另一个 URL 播放音乐时是否应该停止当前的 AVPlayer 实例?

ios - 等待操作完成

iphone - 响应不受支持的实现的选择器方法

iphone - COCOS2D : ScrollView inside a CCScene blocks animation

iphone - 继续执行方法,即使它的 ViewController 在 iOS 中消失

ios - 从自定义相册中删除图像

iphone - 对同一对象多次调用 -retain

ios - CGContextDrawImage 在传递 UIImage.CGImage 时颠倒绘制图像

ios - Swift - GoogleMaps SDK 获取单指或双指点击手势

javascript - 使用固定 ip 从本地主机响应 native 获取