URLSession delegate success methods not called, but no error

2.1k views Asked by At

I'm using an URLSession dataTask to download a file with a URLSessionDownloadDelegate as a result handler. However, urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) is never called. Instead, I get urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) with the error being nil. When using the completionHandler method instead to perform the task, everything works.

Here's my code:

import UIKit

class ViewController: UIViewController, URLSessionDownloadDelegate {

var downloadTask: URLSessionDataTask?

override func viewDidLoad() {
    super.viewDidLoad()

    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

    let url = URL(string: "https://unsplash.it/200/300/?random")!
    //downloadTask = session.dataTask(with: request)
    downloadTask = session.dataTask(with: url)
    downloadTask!.resume()
}


@IBAction func cancelButtonTapped(_ sender: Any) {
    downloadTask?.cancel()
}

func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
    print("session: didBecomeInvalidWithError: \(error?.localizedDescription)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    print("Your data is here!")
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    let progress = Float(totalBytesWritten / totalBytesExpectedToWrite)
    print("Making progress: \(progress)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    print("session: task: didCompleteWithError: \(error?.localizedDescription)")
    session.finishTasksAndInvalidate()
}

}

The simulator output is

session: task: didCompleteWithError: nil
session: didBecomeInvalidWithError: nil

Thank you in advance.

1

There are 1 answers

2
Sebastian On BEST ANSWER

You should use URLSessionDownloadTask instead of URLSessionDataTask and use background for URLSessionConfiguration as follows:

var downloadTask: URLSessionDownloadTask?
var session: URLSession?

override func viewDidLoad() {
        super.viewDidLoad()


        let configuration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
        session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

        let url = URL(string: "https://unsplash.it/200/300/?random")!
        downloadTask = session?.downloadTask(with: url)
        downloadTask!.resume()

}