IOS 8 进度条延迟下载时更新(快速)



我确实创建了一个用户可以下载一些资产的应用程序。我正在使用NSURLSession下载和跟踪UIProgressView的进度。

我的代码:

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    var progress = ("(((totalBytesWritten * 100) / totalBytesExpectedToWrite))" as NSString).floatValue / 100
    println("progress: (progress)")
    progressBar.setProgress(progress, animated: true)
    progressLabel.text = "(progresso)"     
}

使用此代码,我可以在控制台中看到从 0.0 到 1.0 的值但是我的进度条视图仅在 0.0、0.4、0.9、1.0 处更新。或者类似的东西。

我在Zaph的帮助下找到了答案。

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    var progress = ("(((totalBytesWritten * 100) / totalBytesExpectedToWrite))" as NSString).floatValue / 100
    println("progress: (progress)")
    dispatch_async(dispatch_get_main_queue()) {
        progressBar.setProgress(progress, animated: true)    
    }
}

最新更新