我有一个NSProgressIndicator(进度条),在这样的委托方法中似乎可以完美运行:
func restClient(client: DBRestClient!, uploadProgress progress: CGFloat, forFile destPath: String!, from srcPath: String!) {
if progress == 1.0 {
progressBar.hidden = true
generalAlert("Uploaded", text: "Your File has been saved to your computer and uploaded under: (destPath)")
} else {
progressBar.hidden = false
print(progress) // e.g 0.010213
progressBar.doubleValue = Double(progress)*100
}
}
但是,当放置在带有回调的函数中时,我似乎无法以任何方式修改它(既不接受我的 isHidden = false 调用,也不接受双值更新)。
CloudConvert.convert([
"inputformat": "gif",
"outputformat" : "mov",
"input" : "upload",
"file": name],
progressHandler: { (step, percent, message) -> Void in
print(percentage!) //e.g 10.31451. Called every second
self.progressBar.hidden = false //Doesn't Work
self.progressBar.doubleValue = Double(percentage!) //Doesn't Work
},
completionHandler: { (path, error) -> Void in
if(error != nil) {
self.progressBar.hidden = true
self.generalAlert("ERROR", text: "Your file could not be uploaded")
} else {
self.progressBar.hidden = true
self.generalAlert("MOV Uploaded", text: "Your MOV has been saved to your computer and uploaded")
}
})
谁能提供为什么会这样的想法?
完成处理程序将在后台线程中运行所有内容,并且需要在主线程上进行 UI 调用。
像这样将您的进度条调用包装在调度到主队列中
DispatchQueue.main.async(execute: {
self.progressBar.hidden = false //Should now Work
self.progressBar.doubleValue = Double(percentage!) //Should now Work
})
尝试将 UI 代码放在 DispatchQueue.main.async 块中。UI 操作 关闭主线程会出现延迟。