进度视图未更新 - Swift

时间:2015-03-17 03:32:24

标签: ios swift

我正在使用Swift中的ProgressView

这是我的代码

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)

即使complete提供此输出

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

此处不会更新进度视图

self.Progress.setProgress(Float(complete), animated: true)

1 个答案:

答案 0 :(得分:11)

针对Swift 3进行了更新

UI的更改应始终发生在主/前台线程上,在这种情况下,它发生在后台队列上。使用:

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

将其移至前台。

Swift 3之前的旧答案

UI的更改应始终发生在主/前台线程上,在这种情况下,它发生在后台队列上。使用:

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

将其移至前台。