UI swift 中的更改,CoreAnimation:警告,已删除具有未提交 CATransaction 的线程

UI Changes in swift, CoreAnimation: warning, deleted thread with uncommitted CATransaction

我对 swift 比较陌生,想知道是否有人可以帮助解决这个问题。

我试图在服务调用期间将按钮上的标签更改为加载微调器,然后在不久之后更改为该调用的响应消息。

我在日志中收到此错误:

CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

感谢您的帮助。我已经阅读了这些核心动画错误,但我不确定我做错了什么,因为这里的所有内容都是异步完成的。

这是更正后的代码,谢谢@Pierce:

        self.pastebinButton.isEnabled = false
        self.pastebinButton.title = ""
        self.pastebinProgressIndicator.startAnimation(nil)

        pastebinAPI.postPasteRequest(urlEscapedContent: urlEscapeText(txt: text)) { pasteResponse in

            DispatchQueue.main.async {
                self.pastebinProgressIndicator.stopAnimation(nil)
                if pasteResponse.isEmpty {
                    self.pastebinButton.title = "Error"
                } else {
                    self.pastebinButton.title = "Copied!"
                }
            }

            DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
                self.pastebinButton.title = "Pastebin"
                self.pastebinButton.isEnabled = true
            })

所以您甚至在移出主线程之前就调用了 DispatchQueue.main.async。这是不必要的。此外,一旦你在后台线程上工作,你就会更新一些 UI (你的按钮标题)而不分派回主线程。永远不要在后台线程上更新 UI。

if !text.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).isEmpty {

    self.pastebinButton.title = ""
    self.pastebinProgressIndicator.startAnimation(nil)

    pastebinAPI.postPasteRequest(urlEscapedContent: urlEscapeText(txt: text)) { pasteResponse in

       // Clean up your DispatchQueue blocks
       DispatchQueue.main.async {
           self.pastebinProgressIndicator.stopAnimation(nil)
           if pasteResponse.isEmpty {
               self.pastebinButton.title = "Error"
           } else {
               self.pastebinButton.title = "Copied!"
           }
       }

       DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
           self.pastebinButton.title = "Pastebin"
           self.pastebinButton.isEnabled = true
       })

    }
} else {
    Utility.playFunkSound()
}