在swift 3中下载文件时,alamo fire中的进度视图不会更新 [英] progress view in alamo fire will not update when downloading file in swift 3
问题描述
我想下载带有alamo fire的文件并使用带有进度的警报来显示该文件,但是当alamo fire progress.fractionCompleted增加时,进度将不会移动。我使用静态var来等于progres.fractionCompleted,但它不能正常工作
这是我的代码
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 **
推荐答案
我认为您的代码是错误的:根据最新 Alamofire文档我举一个例子来说明如何修改代码:
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...
}
}
这篇关于在swift 3中下载文件时,alamo fire中的进度视图不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!