Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous NSURLConnection and NSOperation - cancelling

I want to cancel all requests. Here is how I'm creating asynchronous connection:

[NSURLConnection sendAsynchronousRequest:echo queue:self.queue completionHandler:^(NSURLResponse *respone, NSData *data, NSError *error){

I then use this method:

-(void)cancelAllRequests
{
    NSLog(@"%@",self.queue.operations);
    [self.queue cancelAllOperations];
    [self.queue waitUntilAllOperationsAreFinished];
}

to cancel all requests.

Which actually doesn't do anything except changing a BOOL to YES.

So how I'm supposed to cancel an asynchronous connection?

like image 424
Devfly Avatar asked Sep 27 '12 15:09

Devfly


2 Answers

You can't cancel connections scheduled using sendAsynchronousRequest. The queue you're referring to is only used for scheduling the completion hander.

If you want full control of the NSURLConnection, you'll have to implement the NSURLConnectionDelegate yourself. An example implementation can be found on https://gist.github.com/3794804

like image 78
leo Avatar answered Oct 20 '22 14:10

leo


What you could do is put Synchronous requests into an Operation (using a block).

The set the NSOperationQueue maxNumberOfConcurrentOperations to 1 (so they run one at a time).

Then if you run cancelAllOperations on the queue it will stop any operations that haven't run yet.

like image 26
Fogmeister Avatar answered Oct 20 '22 15:10

Fogmeister