I have ASINetworkQueue with more than 1500 requests in it. Performing this number of requests takes for a while. If user leaves view controller while this queue is running the OS deallocates the view controller and I get "message sent to deallocated instance" error.
I have tried to use
[self.queue cancelAllOperations];
in dealloc method, but seems like it cancels only requests that are waiting in queue, not the request that is currently running and I'm getting the same error.
What is the correct way to handle this situation? Is it possible to make the view controller not to be deallocated while queue is not finished even if user left it? Or is there a way to cancel all requests (including requests that are running) in queue?
The suggestions by @darvids0n and @AlexReynolds are both good.
ReplyDeleteThe problem is probably that the delegate for the current request is still set, so it tries to notify the (now deallocated) delegated that it's been canceled.
You can most likely avoid this by setting all the delegates to nil instead:
for (ASIHTTPRequest *req in queue.operations)
{
[req setDelegate:nil];
[req cancel];
}
[queue setDelegate:nil];
Detach the network queue's lifespan from that of the view controller. For example, have it managed by the application delegate, which should be alive as long as the application is alive.
ReplyDeleteWhen the view controller is dismissed, cancel all operations in the app delegate's network queue. Even if the view controller is dead, the app delegate should keep chugging along, giving enough time for the network queue to do its cleanup work.
If you think you'll have multiple queues, keep references to them in an array or dictionary with some identifier or index so that you can keep track of them separate from their respective view controllers.