简体   繁体   English

NSOperation需要额外保留

[英]Extra retain needed on NSOperation

I'm developing an iPad app. 我正在开发一款iPad应用程序。 It uses an NSOperation to download something in the background, processed by an NSOperationQueue. 它使用NSOperation在后台下载一些内容,由NSOperationQueue处理。 I'm finding that, unless I add a retain to the NSOperation, I hit a crash after the operation's action is performed. 我发现,除非我在NSOperation中添加一个保留,否则我会在执行操作后发生崩溃。 NSOperationQueue's addOperation says it retains the NSOperation, and the NSOperation itself retains the target object. NSOperationQueue的addOperation表示它保留了NSOperation,而NSOperation本身保留了目标对象。 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). 我还不能在iPhone OS 4.0上开发(提供waitUntilFinished),所以我需要另一种方式知道释放我的额外保留是安全的(或找到一种方法没有额外的保留)。

any suggestions? 有什么建议?

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. 如果您使用的是调用NSOperation对象的异步方法,那可能就是您的问题。 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. 我的猜测是,在你的NSOperation的主要方法中,你开始使用异步方法下载,并且该方法应该在以后回调你的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. 问题是,一旦你的main方法返回,NSOperation被认为已经完成,所以当你的异步下载尝试回调你的NSOperation时,它会失败,因为NSOperation已经完成并取消分配。 Your extra retain fixes this by keeping the NSOperation around even after the NSOperationQueue is finished with it. 你的额外保留通过在NSOperationQueue结束后保持NSOperation来解决这个问题。 You have to fix this by using a synchronous download method inside your NSOperation. 您必须通过在NSOperation中使用同步下载方法来解决此问题。

If you are already using a synchronous download method in your NSOperation, then I'm not sure what your problem could be. 如果您已经在NSOperation中使用同步下载方法,那么我不确定您的问题是什么。

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. 在框架内使用队列和异步请求ASIHTTPRequest - http://allseeing-i.com/ASIHTTPRequest/ - 帮助我解决了NSOperationQueues和许多异步下载时遇到的许多问题。

I take the attitude that if a problem has been solved, and been solved well, don't try and solve it again! 我认为如果问题得到解决并得到妥善解决,请不要再尝试解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM