View/Window 在@IBAction 完成之前不会更新

View/Window won't update until @IBAction is complete

我有一个链接到 IBAction 的按钮,大约需要 5-10 秒才能完成。 虽然 IBAction 函数是 运行,但我想在我的 view/window 中打印进度消息。 唯一打印的消息是最后一条,它在 IBAction 完成后打印(即按钮不是蓝色)。 当我在调试器中单步执行函数时,在我退出函数之前看不到任何消息(即按钮不是蓝色)。

知道如何在执行回调时更新 window 吗?

在后台队列中嵌入 IBAction 逻辑:

DispatchQueue.global(qos: .background).async {
   // background code
}

并且任何时候您需要在 view/window 中打印进度时,请将那部分嵌入其中:

DispatchQueue.main.async {
   // user interface code
}    

通常你可能想试试这个:

DispatchQueue.global(qos: .background).async {
   //background code

   DispatchQueue.main.async {
     //your main thread
   }    
}