[英]download rate using Alamofire
我正在使用此功能編寫帶有alamofire模塊的下載器應用程序,我想以MB / s為單位顯示當前下載速率,我真的不知道如何實現這一目標,請幫幫我。
@IBAction func tapStartButton(_ sender: Any) {
let fileUrl = self.getSaveFileUrl(fileName: Data[0] as String)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
return (fileUrl, [.removePreviousFile, .createIntermediateDirectories])
}
self.request = Alamofire.download(Data[0] as String , to:destination)
.downloadProgress { (progress) in
self.progressCircle.progress = progress.fractionCompleted
cell.progressLabel.isHidden = false
}
.responseData { (data) in
self.Data.removeFirst()
self.startButton.isHidden = false
self.pauseButton.isHidden = true
}
我認為Alamofire或其他任何圖書館都不提供下載速度。 開發人員必須自己計算。 您可以按照以下方式進行操作:
NSTimer
以1秒的間隔來計算速度。 代碼示例:
var prevDownloadedBytes: Int = 0
var totalDownloadedBytes: Int = 0
func calculateDownloadSpeed(){
Timer.scheduleWith(timeInterval: 1.0, repeats: true){
speed = totalDownloadedBytes - prevDownloadedBytes
print("Speed is: \(speed) bps")
prevDownloadedBytes = totalDownloadedBytes
}
}
@IBAction func tapStartButton(_ sender: Any) {
self.request = Alamofire.download(Data[0] as String , to:destination)
.downloadProgress { (progress) in
//Set Total Downloaded bytes here
self.totalDownloadedBytes = progress.fileCompletedCount
self.progressCircle.progress = progress.fractionCompleted
cell.progressLabel.isHidden = false
}
.responseData { (data) in
self.Data.removeFirst()
self.startButton.isHidden = false
self.pauseButton.isHidden = true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.