【问题标题】:NSURLConnection cancellation callbackNSURLConnection 取消回调
【发布时间】:2016-08-19 01:28:18
【问题描述】:

是否可以使用回调捕获NSURLConnection cancel

如果我使用此代码

-(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 比进度 UI 显示。

编辑 问题是关于

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

正确的NSQueueNSOperationQueue *tempQueue = [NSOperationQueue mainQueue];

【问题讨论】:

    标签: ios objective-c nsurlconnection nsconnection


    【解决方案1】:

    是否可以使用回调捕获 NSURLConnection 取消?

    没有。

    来自官方文档here

    调用此方法后,连接不再进行委托方法调用。

    这意味着您应该在调用 cancel 后立即处理 UI 清理,而不是依赖于 _cancelled 变量,因为预计不会再调用 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    我的建议是从您的取消代码中调用 清理方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多