Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress view is not updating - Swift

Tags:

ios

swift

I am working on ProgressView in Swift

Here's my code

var request = HTTPTask()
            let downloadTask = request.download(urlm!, parameters: nil, progress: {(complete: Double) in
                println("percent complete: \(Float(complete))")
                self.Progress.setProgress(Float(complete), animated: true)

Even though complete gives this output

percent complete: 0.00480848
percent complete: 0.0089619
percent complete: 0.0132512
percent complete: 0.0175405
percent complete: 0.0221036
percent complete: 0.0264841
percent complete: 0.0308647
percent complete: 0.0350627
percent complete: 0.0392608
percent complete: 0.0434588
percent complete: 0.0513073
percent complete: 0.0555054
percent complete: 0.0598859
percent complete: 0.0642665
percent complete: 0.0688296
percent complete: 0.0731189
percent complete: 0.0774994
percent complete: 0.0817887
percent complete: 0.0861693
percent complete: 0.0908237
percent complete: 0.095478
percent complete: 0.099676
percent complete: 0.104057
percent complete: 0.109624
percent complete: 0.114278
percent complete: 0.119024
percent complete: 0.123404
percent complete: 0.127602
percent complete: 0.131983
percent complete: 0.136181
percent complete: 0.136408
percent complete: 0.140561
percent complete: 0.144759
percent complete: 0.148957
percent complete: 0.153155
percent complete: 0.157901
percent complete: 0.162282
percent complete: 0.166662
percent complete: 0.171043
percent complete: 0.175241
percent complete: 0.175468
percent complete: 0.179621
percent complete: 0.184002
percent complete: 0.188291
percent complete: 0.192672
percent complete: 0.197052
percent complete: 0.201524
percent complete: 0.205905
percent complete: 0.210285
percent complete: 0.21494
percent complete: 0.215258
...
percent complete: 1.0

It doesn't update progress view here

self.Progress.setProgress(Float(complete), animated: true)
like image 368
Utku Dalmaz Avatar asked Mar 17 '15 03:03

Utku Dalmaz


1 Answers

Updated for Swift 3

Changes to the UI should always occur on the main/foreground thread, in this case it's happening on a background queue. Use:

DispatchQueue.main.async {
    self.Progress.setProgress(Float(complete), animated: true)
}

to move it to the foreground.

Old Answer for pre-Swift 3

Changes to the UI should always occur on the main/foreground thread, in this case it's happening on a background queue. Use:

dispatch_async(dispatch_get_main_queue()) {
    self.Progress.setProgress(Float(complete), animated: true)
}

to move it to the foreground.

like image 159
David Berry Avatar answered Sep 19 '22 18:09

David Berry