简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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