Skip to main content

How safely cancel all requests in ASINetworkQueue on view deallocate



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?


Comments

  1. The suggestions by @darvids0n and @AlexReynolds are both good.

    The 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];

    ReplyDelete
  2. 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.

    When 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.