Skip to content

Progress handler values are incorrect(?) on resumed downloads #187

@j531

Description

@j531

I use the values from the progress handler to set the progress in a loading bar. This works great for the first download of an image but then produces what feel like semantically incorrect values on resumed downloads of that same image.

To reproduce:

  • Start a download of an image.
  • Cancel the task mid way through the download.
  • Start a download of the same image.
  • Note that the total data remaining passed to the progress handler is now the value remaining of the download, not the total value of the image. While technically correct, this feels semantically incorrect for the purpose of the progress handler because the 'progress' calculation here (completed / total) now produces values over 100%.

Simple example:

class PrototypeViewController: UIViewController {

    private let imageView = UIImageView()
    private var imageTask: ImageTask?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(imageView)

        let startButton = UIButton()
        view.addSubview(startButton)
        startButton.setTitle("Start", for: .normal)
        startButton.addTarget(self, action: #selector(startDownload), for: .touchUpInside)
        let buttonHeight: CGFloat = 40
        startButton.frame = CGRect(x: 0, y: buttonHeight, width: view.bounds.width, height: buttonHeight)

        let cancelButton = UIButton()
        view.addSubview(cancelButton)
        cancelButton.setTitle("Cancel", for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelDownload), for: .touchUpInside)
        cancelButton.frame = CGRect(x: 0, y: buttonHeight * 2, width: view.bounds.width, height: buttonHeight)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        imageView.frame = view.bounds
    }

    @objc private func startDownload() {
        let url = URL(string: "https://apod.nasa.gov/apod/image/1809/Broom_Pickering_milne_APODw1200.jpg")!

        imageTask = Nuke.loadImage(
            with: url,
            into: imageView,
            progress: { _, completed, total in
                let progress = (CGFloat(completed) / CGFloat(total)) * 100
                print("Progress:", progress, "Completed:", completed, "Total:", total)
            },
            completion: { (response, error) in
                print("Completed")
        })
    }

    @objc private func cancelDownload() {
        imageTask?.cancel()
    }
}

Is there a setting I can change to alter this behaviour?

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions