5

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?

4

2 回答 2

5

无法取消使用 安排的连接sendAsynchronousRequest。您所指的队列仅用于调度完成处理程序。

如果您想完全控制 ,则NSURLConnection必须NSURLConnectionDelegate自己实现。示例实现可以在https://gist.github.com/3794804上找到

于 2012-09-27T15:20:07.957 回答
1

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.

于 2012-09-27T15:24:55.477 回答