Swift UIProgressView进度更新在AlamoFire(异步)调用中非常慢

Swift UIProgressView进度更新在AlamoFire(异步)调用中非常慢,swift,uiprogressview,alamofire,nsnotification,dispatch-async,Swift,Uiprogressview,Alamofire,Nsnotification,Dispatch Async,在AlamoFire get请求中,我正在尝试更新我的进度条。像这样: alamofireManager.request(.GET, urlPath, parameters: params).responseJSON{(request,response,JSON,error) in ...<code here>... dispatch_async(dispatch_get_main_queue(), { NSNotificationCenter.def

在AlamoFire get请求中,我正在尝试更新我的进度条。像这样:

alamofireManager.request(.GET, urlPath, parameters: params).responseJSON{(request,response,JSON,error) in
    ...<code here>...
    dispatch_async(dispatch_get_main_queue(), {
        NSNotificationCenter.defaultCenter().postNotificationName(LoginVCHideSpinnerAndShowProgressBarName as String, object: self)
    })
    ...<more code here>...
}
这似乎比通知慢

dispatch_async(dispatch_get_main_queue(), {
    loginVC.progressBar.hidden = false
    loginVC.indicator.hidden = true
    loginVC.progressBar.setProgress(0.1, animated: true)
})

我很困惑为什么会发生这种情况,因为我告诉它在主线程中更新。我还不明白为什么通知实际上要快一点。

在Alamofire中有一种更好的方法可以做到这一点。当您的数据任务发生数据传输时,您可以使用
progress
闭包自动获取回调。下面是自述文件监视下载请求进度的一个示例。这同样适用于数据请求

let progressView = UIProgressView(progressViewStyle: .Bar)

let params = ["foo": "bar"]
let URLString = "http://httpbin.org/get"

let request = Alamofire.request(.GET, URLString, parameters: params)
request.progress { _, _, _ in
    progressView.setProgress(request.progress.fractionCompleted, animated: true)
}
request.responseJSON { request, response, json, error in
    println(request)
    println(response)
    println(json)
    println(error)
}

谢谢:)很抱歉迟到了,我在度假不用担心…很高兴它能帮上忙!