2012-09-27 81 views
5

我想取消所有请求。下面是我如何创建异步连接:异步NSURLConnection和NSOperation - 取消

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

然后我用这个方法:

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

取消所有请求。

除了将BOOL更改为YES之外,哪个实际上不执行任何操作。

那么我该如何取消异步连接呢?

回答

5

不能取消连接计划使用sendAsynchronousRequest。您所指的队列仅用于安排完成手柄。

如果你想完全控制NSURLConnection,你必须自己实现NSURLConnectionDelegate。一个示例实现可以在https://gist.github.com/3794804

+0

我会在几分钟内分享这个要点。 – leo

1

你可以做什么是将同步请求放入操作(使用块)。

将NSOperationQueue maxNumberOfConcurrentOperations设置为1(因此它们一次只运行一个)。

然后,如果您在队列上运行cancelAllOperations,它将停止任何尚未运行的操作。