简体   繁体   English

URLSession委托未调用的成功方法,但没有错误

[英]URLSession delegate success methods not called, but no error

I'm using an URLSession dataTask to download a file with a URLSessionDownloadDelegate as a result handler. 我正在使用URLSession dataTask下载一个带有URLSessionDownloadDelegate的文件作为结果处理程序。 However, urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) is never called. 但是,永远不会调用urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) Instead, I get urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) with the error being nil. 相反,我得到urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) ,错误为nil。 When using the completionHandler method instead to perform the task, everything works. 当使用completionHandler方法来执行任务时,一切正常。

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. 先感谢您。

You should use URLSessionDownloadTask instead of URLSessionDataTask and use background for URLSessionConfiguration as follows: 您应该使用URLSessionDownloadTask而不是URLSessionDataTask并使用背景进行URLSessionConfiguration ,如下所示:

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()

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM