Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLSession didCompleteWithError nil 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?

like image 281
Stan Wiechers Avatar asked Apr 08 '16 17:04

Stan Wiechers


1 Answers

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

like image 119
christian mini Avatar answered Nov 17 '22 10:11

christian mini