NSOperation 需要额外保留

发布于 2024-09-07 07:58:24 字数 366 浏览 8 评论 0原文

我正在开发一个 iPad 应用程序。它使用 NSOperation 在后台下载某些内容,并由 NSOperationQueue 处理。我发现,除非我向 NSOperation 添加保留,否则在执行操作的操作后我会发生崩溃。 NSOperationQueue的addOperation说它保留​​了NSOperation,而NSOperation本身保留了目标对象。因此,我不明白为什么需要额外的保留。

这不会阻碍我的进步,除非我试图实现取消异步操作并在其位置创建一个新操作的模式。我还不能在 iPhone OS 4.0 上进行开发(它提供了 waitUntilFinished),所以我需要另一种方式来知道释放我的额外保留是安全的(或者找到一种根本没有额外保留的方法)。

有什么建议吗?

I'm developing an iPad app. It uses an NSOperation to download something in the background, processed by an NSOperationQueue. I'm finding that, unless I add a retain to the NSOperation, I hit a crash after the operation's action is performed. NSOperationQueue's addOperation says it retains the NSOperation, and the NSOperation itself retains the target object. Hence, I'm not understanding why my extra retain is needed.

This wouldn't hinder my progress, except I am trying to implement the pattern of cancelling the async operation and creating a new one in its place. I can't develop on iPhone OS 4.0 as yet (which provides waitUntilFinished), so i need another way of knowing it is safe to release my extra retain (or find a way to not have the extra retain at all).

any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-09-14 07:58:24

您如何执行“在后台下载某些内容”任务?如果您使用回调 NSOperation 对象的异步方法,那可能就是您的问题。我的猜测是,在 NSOperation 的 main 方法中,您正在使用异步方法开始下载,并且该方法应该稍后回调到您的 NSOperation。问题是,一旦你的 main 方法返回,NSOperation 就被认为完成了,所以当你的异步下载尝试回调你的 NSOperation 时,它会失败,因为 NSOperation 已经完成并释放了。即使 NSOperationQueue 完成后,您的额外保留也会通过保留 NSOperation 来修复此问题。您必须通过在 NSOperation 中使用同步下载方法来解决此问题。

如果您已经在 NSOperation 中使用同步下载方法,那么我不确定您的问题是什么。

How are you performing the 'download something in the background' task? If you are using an async method that calls back to your NSOperation object, that is probably your problem. My guess is that in your NSOperation's main method, you are beginning the download using an async method, and that method is supposed to later on call back to your NSOperation. The problem is, once your main method returns, the NSOperation is considered finished, so when your async download tries calling back to your NSOperation, it will fail because the NSOperation has already finished and deallocated. Your extra retain fixes this by keeping the NSOperation around even after the NSOperationQueue is finished with it. You have to fix this by using a synchronous download method inside your NSOperation.

If you are already using a synchronous download method in your NSOperation, then I'm not sure what your problem could be.

梦与时光遇 2024-09-14 07:58:24

在框架 ASIHTTPRequest 中使用队列和异步请求 - http://allseeing-i.com/ASIHTTPRequest/ - 帮助我解决了 NSOperationQueues 和许多异步下载所遇到的很多问题。

我的态度是,如果问题已经解决,并且解决得很好,就不要再尝试解决它!

Using the queues and async requests within the framework ASIHTTPRequest - http://allseeing-i.com/ASIHTTPRequest/ - helped me solve a lot of the problems I was having with NSOperationQueues and many asynchronous downloads.

I take the attitude that if a problem has been solved, and been solved well, don't try and solve it again!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文