progress view in alamo fire will not update when downloading file in swift 3

十年热恋 提交于 2019-12-02 19:17:54

问题


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...
                }
}


来源:https://stackoverflow.com/questions/46070516/progress-view-in-alamo-fire-will-not-update-when-downloading-file-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!