2016-04-25 94 views
0

是否有可能使用回调赶上NSURLConnection cancelNSURLConnection取消回调

如果我使用此代码

-(void) pleaseStopDownload { 
cancelled = YES; 
    [conn cancel]; 
    conn = nil; 
    [self myUpdateUImessage]; 
} 

不时myUpdateUImessage这个回调

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"didReceiveData"); 
if (!cancelled) { 
//this code inside brackets suddenly is calling:(
//not always but from time to time 
    summ += data.length; 
    if (_progressHandler != nil) 
     _progressHandler(data, summ, max); 
} else { 
return; 
} 
} 

,所以用户界面不能正确更新之前调用!也就是说,最终用户界面显示的是进度UI。

编辑 的问题是关于

NSOperationQueue *tempQueue = [[NSOperationQueue alloc] init]; 
[conn setDelegateQueue:tempQueue]; 

正确NSQueueNSOperationQueue *tempQueue = [NSOperationQueue mainQueue];

回答

2

是否有可能赶上NSURLConnection的使用取消回调?

从官方文档here:这种方法被称为

后,连接不再有进一步的委托方法调用。

这意味着你应该尽快处理UI清理您拨打cancel因为- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data预计不会再被调用不能依靠_cancelled变量。

我的意见,是从你的取消代码调用清理方法

-(void) pleaseStopDownload { 
    [conn cancel]; 
    conn = nil; 
    [self handleCancelledDownload]; 
}