从swift中的dataTaskWithURL获得进度

taf*_*rel 15 ios swift

dataTaskWithURL在数据下载时有没有办法从swift 获得进展?

NSURLSession.sharedSession().dataTaskWithURL(...)
Run Code Online (Sandbox Code Playgroud)

我需要在下载数据时显示进度条.

Dha*_*esh 33

您可以使用此代码显示带有委托功能的进度条的下载过程.

import UIKit

class ViewController: UIViewController,NSURLSessionDelegate,NSURLSessionDataDelegate{

    @IBOutlet weak var progress: UIProgressView!

    var buffer:NSMutableData = NSMutableData()
    var session:NSURLSession?
    var dataTask:NSURLSessionDataTask?
    let url = NSURL(string:"http://i.stack.imgur.com/b8zkg.png" )!
    var expectedContentLength = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        progress.progress = 0.0
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let manqueue = NSOperationQueue.mainQueue()
        session = NSURLSession(configuration: configuration, delegate:self, delegateQueue: manqueue)
        dataTask = session?.dataTaskWithRequest(NSURLRequest(URL: url))
        dataTask?.resume()

        // Do any additional setup after loading the view, typically from a nib.
    }
    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {

        //here you can get full lenth of your content
        expectedContentLength = Int(response.expectedContentLength)
        println(expectedContentLength)
        completionHandler(NSURLSessionResponseDisposition.Allow)
    }
    func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {


        buffer.appendData(data)

        let percentageDownloaded = Float(buffer.length) / Float(expectedContentLength)
        progress.progress =  percentageDownloaded
    }
    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        //use buffer here.Download is done
        progress.progress = 1.0   // download 100% complete
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供有用的示例.如果我想将NSURLSession逻辑隔离到一个单独的服务访问层并且仍然更新了Progress,那么您是否可以或者能够共享该场景的任何示例?谢谢. (2认同)

Vla*_*lad 18

Swift4的更新:
支持执行多个同时操作.

档案:DownloadService.swift.保持对URLSession的引用并跟踪执行的任务.

final class DownloadService: NSObject {

   private var session: URLSession!
   private var downloadTasks = [GenericDownloadTask]()

   public static let shared = DownloadService()

   private override init() {
      super.init()
      let configuration = URLSessionConfiguration.default
      session = URLSession(configuration: configuration,
                           delegate: self, delegateQueue: nil)
   }

   func download(request: URLRequest) -> DownloadTask {
      let task = session.dataTask(with: request)
      let downloadTask = GenericDownloadTask(task: task)
      downloadTasks.append(downloadTask)
      return downloadTask
   }
}


extension DownloadService: URLSessionDataDelegate {

   func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
                   completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

      guard let task = downloadTasks.first(where: { $0.task == dataTask }) else {
         completionHandler(.cancel)
         return
      }
      task.expectedContentLength = response.expectedContentLength
      completionHandler(.allow)
   }

   func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
      guard let task = downloadTasks.first(where: { $0.task == dataTask }) else {
         return
      }
      task.buffer.append(data)
      let percentageDownloaded = Double(task.buffer.count) / Double(task.expectedContentLength)
      DispatchQueue.main.async {
         task.progressHandler?(percentageDownloaded)
      }
   }

   func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
      guard let index = downloadTasks.index(where: { $0.task == task }) else {
         return
      }
      let task = downloadTasks.remove(at: index)
      DispatchQueue.main.async {
         if let e = error {
            task.completionHandler?(.failure(e))
         } else {
            task.completionHandler?(.success(task.buffer))
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

档案:DownloadTask.swift.轻量级界面只是为了隐藏具体的实现.

protocol DownloadTask {

   var completionHandler: ResultType<Data>.Completion? { get set }
   var progressHandler: ((Double) -> Void)? { get set }

   func resume()
   func suspend()
   func cancel()
}
Run Code Online (Sandbox Code Playgroud)

档案:GenericDownloadTask.swift.具体实现DownloadTask接口.

class GenericDownloadTask {

   var completionHandler: ResultType<Data>.Completion?
   var progressHandler: ((Double) -> Void)?

   private(set) var task: URLSessionDataTask
   var expectedContentLength: Int64 = 0
   var buffer = Data()

   init(task: URLSessionDataTask) {
      self.task = task
   }

   deinit {
      print("Deinit: \(task.originalRequest?.url?.absoluteString ?? "")")
   }

}

extension GenericDownloadTask: DownloadTask {

   func resume() {
      task.resume()
   }

   func suspend() {
      task.suspend()
   }

   func cancel() {
      task.cancel()
   }
}
Run Code Online (Sandbox Code Playgroud)

档案:ResultType.swift.保持结果或错误的可重用类型.

public enum ResultType<T> {

   public typealias Completion = (ResultType<T>) -> Void

   case success(T)
   case failure(Swift.Error)

}
Run Code Online (Sandbox Code Playgroud)

用法:示例如何运行two download tasks in parallel(macOS App):

class ViewController: NSViewController {

   @IBOutlet fileprivate weak var loadImageButton1: NSButton!
   @IBOutlet fileprivate weak var loadProgressIndicator1: NSProgressIndicator!
   @IBOutlet fileprivate weak var imageView1: NSImageView!

   @IBOutlet fileprivate weak var loadImageButton2: NSButton!
   @IBOutlet fileprivate weak var loadProgressIndicator2: NSProgressIndicator!
   @IBOutlet fileprivate weak var imageView2: NSImageView!

   fileprivate var downloadTask1:  DownloadTask?
   fileprivate var downloadTask2:  DownloadTask?

   override func viewDidLoad() {
      super.viewDidLoad()
      loadImageButton1.target = self
      loadImageButton1.action = #selector(startDownload1(_:))
      loadImageButton2.target = self
      loadImageButton2.action = #selector(startDownload2(_:))
   }

}


extension ViewController {

   @objc fileprivate func startDownload1(_ button: NSButton) {
      let url = URL(string: "http://localhost:8001/?imageID=01&tilestamp=\(Date.timeIntervalSinceReferenceDate)")!
      let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
      downloadTask1 = DownloadService.shared.download(request: request)
      downloadTask1?.completionHandler = { [weak self] in
         switch $0 {
         case .failure(let error):
            print(error)
         case .success(let data):
            print("Number of bytes: \(data.count)")
            self?.imageView1.image = NSImage(data: data)
         }
         self?.downloadTask1 = nil
         self?.loadImageButton1.isEnabled = true
      }
      downloadTask1?.progressHandler = { [weak self] in
         print("Task1: \($0)")
         self?.loadProgressIndicator1.doubleValue = $0
      }

      loadImageButton1.isEnabled = false
      imageView1.image = nil
      loadProgressIndicator1.doubleValue = 0
      downloadTask1?.resume()
   }

   @objc fileprivate func startDownload2(_ button: NSButton) {
      let url = URL(string: "http://localhost:8002/?imageID=02&tilestamp=\(Date.timeIntervalSinceReferenceDate)")!
      let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
      downloadTask2 = DownloadService.shared.download(request: request)
      downloadTask2?.completionHandler = { [weak self] in
         switch $0 {
         case .failure(let error):
            print(error)
         case .success(let data):
            print("Number of bytes: \(data.count)")
            self?.imageView2.image = NSImage(data: data)
         }
         self?.downloadTask2 = nil
         self?.loadImageButton2.isEnabled = true
      }
      downloadTask2?.progressHandler = { [weak self] in
         print("Task2: \($0)")
         self?.loadProgressIndicator2.doubleValue = $0
      }

      loadImageButton2.isEnabled = false
      imageView2.image = nil
      loadProgressIndicator2.doubleValue = 0
      downloadTask2?.resume()
   }
}
Run Code Online (Sandbox Code Playgroud)

奖金1.档案StartPHPWebServer.command.运行2内置PHP服务器以模拟同时下载的示例脚本.

#!/bin/bash

AWLScriptDirPath=$(cd "$(dirname "$0")"; pwd)
cd "$AWLScriptDirPath"

php -S localhost:8001 &
php -S localhost:8002 &

ps -afx | grep php
echo "Press ENTER to exit."
read
killall php
Run Code Online (Sandbox Code Playgroud)

奖金2.档案index.php.用于实现慢速下载的示例PHP脚本.

<?php

$imageID = $_REQUEST["imageID"];
$local_file = "Image-$imageID.jpg";

$download_rate = 20.5; // set the download rate limit (=> 20,5 kb/s)
if (file_exists($local_file) && is_file($local_file)) {
    header('Cache-control: private');
    header('Content-Type: image/jpeg');
    header('Content-Length: '.filesize($local_file));

    flush();
    $file = fopen($local_file, "r");
    while(!feof($file)) {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        flush(); // flush the content to the browser
        usleep(0.25 * 1000000);
    }
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}

?>
Run Code Online (Sandbox Code Playgroud)

杂项:模拟2台PHP服务器的目录内容.

Image-01.jpg
Image-02.jpg
StartPHPWebServer.command
index.php
Run Code Online (Sandbox Code Playgroud)


yas*_*375 16

您可以简单地观察对象的progress属性URLSessionDataTask

例:

import UIKit

class SomeViewController: UIViewController {

  private var observation: NSKeyValueObservation?

  deinit {
    observation?.invalidate()
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "https://source.unsplash.com/random/4000x4000")!
    let task = URLSession.shared.dataTask(with: url)

    observation = task.progress.observe(\.fractionCompleted) { progress, _ in
      print("progress: ", progress.fractionCompleted)
    }

    task.resume()
  }
}
Run Code Online (Sandbox Code Playgroud)

操场上的例子:

import Foundation
import PlaygroundSupport

let page = PlaygroundPage.current
page.needsIndefiniteExecution = true

let url = URL(string: "https://source.unsplash.com/random/4000x4000")!
let task = URLSession.shared.dataTask(with: url) { _, _, _ in
  page.finishExecution()
}

// Don't forget to invalidate the observation when you don't need it anymore.
let observation = task.progress.observe(\.fractionCompleted) { progress, _ in
  print(progress.fractionCompleted)
}

task.resume()
Run Code Online (Sandbox Code Playgroud)

  • 这是迄今为止对问题的最佳答案!不是唯一正确的答案,而是最好的答案。非常简单,它在不使用委托的情况下工作(切换到委托可能会导致其他问题) (3认同)