xandermonkey
xandermonkey

Reputation: 4422

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

I'm relatively new to swift and wondering if anyone could help with this issue.

I'm trying to make the label on a button change to a loading spinner during a service call, and then change to the response message of that call shortly after.

I get this error in my log:

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

Thanks for the help. I've read about these core animation errors, but I'm not sure what I'm doing wrong as everything here is done asynchronously.

Here is the corrected code, thanks @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
            })

Upvotes: 0

Views: 154

Answers (1)

Pierce
Pierce

Reputation: 3158

So you're calling the DispatchQueue.main.async before you've even moved outside the main thread. This is unnecessary. Also once you're working on the background thread you are updating some UI (your button title) without dispatching back to the main thread. Never update UI on a background thread.

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()
}

Upvotes: 1

Related Questions