NSURLSession didCompleteWithError:gets called with NSError is nil

*爱你&永不变心* 提交于 2019-12-01 06:46:05

问题


Scenario is taking the app to background and foreground multiple times when its uploading is in progress,didCompleteWithError: method is called while taking app to foreground with error parameter is null. Probabilty is 1/3.

How do i find out what went wrong as error parameter does not give anything.


回答1:


The problem is that didCompleteWithError report only client side error, otherwise is nil. iOS doc said:

"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 session's errors you have to implement session protocol delegate

- URLSession:didBecomeInvalidWithError:

Remember to invalidate the session after stop using it. So if you create an NSURLSession like this:

NSURLSessionConfiguration *backgroundConfigurationObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"applycasession"];
    self.backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfigurationObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

When you finish to use it you have to call:

 [self.backgroundSession invalidateAndCancel];

Please read the documents about "Life Cycle of URL Session" at this link



来源:https://stackoverflow.com/questions/31262564/nsurlsession-didcompletewitherrorgets-called-with-nserror-is-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!