NSURLConnection取消回调
问题描述:
是否有可能使用回调赶上NSURLConnection cancel
?NSURLConnection取消回调
如果我使用此代码
-(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];
正确NSQueue
是NSOperationQueue *tempQueue = [NSOperationQueue mainQueue];
答
是否有可能赶上NSURLConnection的使用取消回调?
号
从官方文档here:这种方法被称为
后,连接不再有进一步的委托方法调用。
这意味着你应该尽快处理UI清理您拨打cancel
因为- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
预计不会再被调用不能依靠_cancelled
变量。
我的意见,是从你的取消代码调用清理方法:
-(void) pleaseStopDownload {
[conn cancel];
conn = nil;
[self handleCancelledDownload];
}