URLSession didCompleteWithError nil错误 [英] URLSession didCompleteWithError nil error

查看:144
本文介绍了URLSession didCompleteWithError nil错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在正在作为NSURLSessionDelegate的控制器中执行后台URLSession的IOS9应用程序上工作.这是我的启动方式:

    self.session_data = [[NSMutableData alloc] init];
    NSURL *url = [NSURL URLWithString:src];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLSessionConfiguration *backgroundConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"myBackgroundSessionIdentifier"];
    self.session = [NSURLSession sessionWithConfiguration: backgroundConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    self.download = [self.session dataTaskWithRequest: request ];
    [self.download resume];

到目前为止,一切都很好.我实现了三种委托方法.首先调用"didReceiveData",然后存储数据.

- (void)URLSession:(NSURLSession *)session
      dataTask:(NSURLSessionDataTask *)dataTask
     didReceiveData:(NSData *)data{

   NSLog(@"%s",__func__);
   [self.session_data appendData:data];
}

紧随其后的是'didCompleteWithError'被调用.永远不会调用'completionHandler'处理程序.

"didCompleteWithError"消息令人困惑的是,实际的错误对象为nil.我已经看到了一些类似的未解决的问题.加载时我不会离开控制器/视图.我需要将该功能移至AppDelegate吗?

解决方案

Apple文档说 didCompleteWithError 报告客户端错误,否则为nil:

不会通过error参数报告服务器错误.您的代表通过error参数收到的唯一错误是客户端错误,例如无法解析主机名或无法连接到主机."

这是答案

Working on an IOS9 app that is doing a background URLSession in a controller that is a NSURLSessionDelegate. Here is how I start it:

    self.session_data = [[NSMutableData alloc] init];
    NSURL *url = [NSURL URLWithString:src];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLSessionConfiguration *backgroundConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"myBackgroundSessionIdentifier"];
    self.session = [NSURLSession sessionWithConfiguration: backgroundConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    self.download = [self.session dataTaskWithRequest: request ];
    [self.download resume];

So far so good. I implement the three delegate methods. 'didReceiveData' is called first and I store the data.

- (void)URLSession:(NSURLSession *)session
      dataTask:(NSURLSessionDataTask *)dataTask
     didReceiveData:(NSData *)data{

   NSLog(@"%s",__func__);
   [self.session_data appendData:data];
}

Right after that 'didCompleteWithError' is called. The 'completionHandler' handler is never called.

What is confusing about 'didCompleteWithError' message is that the actual error object is nil. I have seen some similar unanswered questions. I am not leaving the controller/view while loading. Do I need to move that functionality into AppDelegate?

解决方案

Apple doc said that didCompleteWithError report only client side error, otherwise is nil:

"Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host."

This is the link to the documentation.

If you want to check other errors like session's errors you have to implement session protocol delegate

- URLSession:didBecomeInvalidWithError:

For more details, see this answer

这篇关于URLSession didCompleteWithError nil错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆