Background download not able to get error response

Viewed 196

Expected Error response in downloading file with background configuration

{
  "ref": "123",
  "msg": "<error message>"
}

if creating Data task I am able to get error in response as expected but if I go through background session not able to get error dictionary.

let url = ....
let destination = ... 
let headers = ... 

        BackgroundSession.shared.manager.download(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers, to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in

        }
        BackgroundSession.shared.manager.delegate.taskDidComplete = { _, task, error in // task completed with error
            if let error = error {
                // ONLY can get Error response width code not expected dictionary 
            }
        }

        BackgroundSession.shared.manager.delegate.downloadTaskDidFinishDownloadingToURL = { _, task, tempLocation in
            if let response = task.response as? HTTPURLResponse {
                 // Do something expected
            }
        }

Only problem is not able to get error dictionary otherwise worked fined with background download

I have also tried downloading through request method of SessionManager then I am getting the error dictionary expected but then downloading functionality not working

Edited In following code it work file for download file and get error dictionary but not getting download file in background.

let url = ....
let headers = ...
 let request = BackgroundSession.shared.manager.request(url, method: .get, parameters: nil, headers: headers).validate(statusCode: 200...201)
         request.responseData { (response) in
           if let resp = response.response {
          switch response.result {
          case .success(let data):

          case .failure:
           var ne = NetworkError(rawValue: resp.statusCode)!
           if let dict = self.convertDataToDictionary(response.data) {
              ne = NetworkError(code: resp.statusCode, json: dict)
           }
            errorCallback(resp, ne)
          }
         }
         } 

Any help will be appreciated

Thanks

0 Answers
Related