ricardo
ricardo

Reputation: 1601

IOS 8 progress bar delay to update while downloading (swift)

I did create an app which the user can download some assets. I am using the NSURLSession to download and track the progress with the UIProgressView.

Folow my code:

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)"     
}

With this code I can see in the console the values from 0.0 to 1.0 But my progress bar view only updates at 0.0, 0.4, 0.9 , 1.0. Or some thing like that.

Upvotes: 0

Views: 1334

Answers (1)

ricardo
ricardo

Reputation: 1601

I found the answer with the help of 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)    

    }

}

Upvotes: 1

Related Questions