代码之家  ›  专栏  ›  技术社区  ›  Julien7377

urlsession如何确定何时下载所有文件

  •  0
  • Julien7377  · 技术社区  · 6 年前

    我正在使用urlsession从服务器下载一些文件,每次下载后都会触发委托“didFinishDownloadingTo”。但是我想要的东西,触发后,所有的下载完成。

    我必须使用的委托是“didCompleteWithError”吗?

    func downloadPdf() {
    
        for k in self.resultAddressServer {
    
    
                let fileURL = URL(string: k)
                let sessionConfig = URLSessionConfiguration.default
                let operationQueue = OperationQueue()
                let urlSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: operationQueue)
                let request = URLRequest(url:fileURL!)
                let downloadTask = urlSession.downloadTask(with: request)
    
                downloadTask.resume()
        }
    }
    
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    
        do {
            let manager = FileManager.default
            let destinationURL = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                .appendingPathComponent(downloadTask.originalRequest!.url!.lastPathComponent)
            try? manager.removeItem(at: destinationURL)
            try manager.moveItem(at: location, to: destinationURL)
            print(destinationURL)
        } catch {
            print(error)
        }
    
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        if error != nil {
            DispatchQueue.main.async() {
                self.statusLabel.text = "Download failed"
            }
    } else {
            DispatchQueue.main.async() {
                self.statusLabel.text = "Download finished"
    
            }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Julien7377    6 年前

    func downloadPdf() {
    
        for k in self.resultAddressServer {
    
    
                let fileURL = URL(string: k)
                let sessionConfig = URLSessionConfiguration.background(withIdentifier: "com.Test")
                let urlSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue())
                let request = URLRequest(url:fileURL!)
                let downloadTask = urlSession.downloadTask(with: request)
                self.tasksArray.append(downloadTask.taskIdentifier)
                print(downloadTask.taskIdentifier)
                downloadTask.resume()
        }
    
    
    
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    
        do {
            let manager = FileManager.default
            let destinationURL = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                .appendingPathComponent(downloadTask.originalRequest!.url!.lastPathComponent)
            try? manager.removeItem(at: destinationURL)
            try manager.moveItem(at: location, to: destinationURL)
            print(destinationURL)
        } catch {
            print(error)
        }
    
        if let index = self.tasksArray.index(of: downloadTask.taskIdentifier) {
            self.tasksArray.remove(at: index)
            print(self.tasksArray.count)
        }
    
        DispatchQueue.main.async() {
    
            if self.tasksArray.count == 0 {
    
                //Do whatever you want, all downloads are completed
                //the DispatchQueue is for what I use this for...maybe not needed in you case
            }
    
    
        }
    }