views:

100

answers:

2

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?

A: 

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.

Gotosleep
A: 

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!

adam