设置自动释放的NSOperationQueue是危险的吗? [英] Is it dangerous to set off an autoreleased NSOperationQueue?

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

问题描述

我有一个任务需要相当长的时间,应该在后台运行。根据文档,这可以使用 NSOperationQueue 来完成。但是,我不想保留 NSOperationQueue 的类全局副本,因为我真的只使用它一个任务。因此,我只是将其设置为自动释放,并希望它不会在任务完成之前释放。它工作。

像这样:

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天全站免登陆