progress view in alamo fire will not update when d

2020-05-08 07:37发布

问题:

I want to download a file with alamo fire and using alert with progress to show it but the progress will not move when alamo fire progress.fractionCompleted increased I use static var to equal progres.fractionCompleted but it doesn't worked too here is my codes

 let destination = DownloadRequest.suggestedDownloadDestination()

        Alamofire.download("example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in
            print("Progress: \(progress.fractionCompleted)")
            sixthLevelViewController.progressdownload = Float(progress.fractionCompleted)

        DispatchQueue.main.async {

            let alertController = UIAlertController(title: "Downloading", message: "please wait", preferredStyle: .alert)

            let progressDownload : UIProgressView = UIProgressView(progressViewStyle: .default)

            progressDownload.setProgress(ViewController.progressdownload, animated: true)
            progressDownload.frame = CGRect(x: 10, y: 70, width: 250, height: 0)

            alertController.view.addSubview(progressDownload)
            self.present(alertController, animated: true, completion: nil)

        }
            } .validate().responseData { ( response ) in
                print(response.destinationURL!.lastPathComponent)
        }

**Remember that this will work well and the problem is just updating progress view **

回答1:

I think your code is wrong: according to the latest Alamofire documents I make an example to show how you can modify your code:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                                                .userDomainMask, true)[0]
        let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
        let fileURL = documentsURL.appendingPathComponent("myfile.pdf")

        return (fileURL, [.removePreviousFile, .createIntermediateDirectories]) }

func requestDownloadInfo(url:String) {
            Alamofire.download(url, to :destination)
                .downloadProgress(queue: utilityQueue) { progress in
                    let progressDic = ["progress" : progress.completedUnitCount, "total" :  progress.totalUnitCount, "fraction" : progress.fractionCompleted] as [String:Any]
                    // here you can show your alert using fraction value..
                }
                .responseData { response in
                  // check your file after download or check errors...
                }
}