触发自动释放的 NSOperationQueue 是否危险? [英] Is it dangerous to set off an autoreleased NSOperationQueue?

查看:33
本文介绍了触发自动释放的 NSOperationQueue 是否危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要很长时间的任务,应该在后台运行.根据文档,这可以使用 NSOperationQueue 来完成.但是,我不想保留 NSOperationQueue 的类全局副本,因为我真的只将它用于一项任务.因此,我只是将它设置为 autorelease 并希望它在任务完成之前不会被释放.它有效.
像这样:

I have a task that takes a rather long time and should run in the background. According to the documentation, this can be done using an NSOperationQueue. However, I do not want to keep a class-global copy of the NSOperationQueue since I really only use it for that one task. Hence, I just set it to autorelease and hope that it won't get released before the task is done. It works.
like this:

NSInvocationOperation *theTask = [NSInvocationOperation alloc];
theTask = [theTask initWithTarget:self
                         selector:@selector(doTask:)
                           object:nil];
NSOperationQueue *operationQueue = [[NSOperationQueue new] autorelease];
[operationQueue addOperation:theTask];
[theTask release];

不过,我有点担心.这能保证有效吗?或者 operationQueue 可能在某个时候被释放并带走 theTask 吗?

I am kind of worried, though. Is this guaranteed to work? Or might operationQueue get deallocated at some point and take theTask with it?

推荐答案

文档中没有说明 NSOperationQueue 被释放时会发生什么.假设不能保证任务会被执行是最安全的.

There's nothing in the documentation to say what happens when the NSOperationQueue is released. It would be safest to assume there's no guarantee that theTask will get executed.

这篇关于触发自动释放的 NSOperationQueue 是否危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆