Getting a "This application is modifying the autolayout engine from a background thread" error?

Mark picture Mark · Feb 3, 2015 · Viewed 151.5k times · Source

Been encountering this error a lot in my OS X using swift:

"This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release."

I have a my NSWindow and I'm swapping in views to the contentView of the window. I get the error when I try and do a NSApp.beginSheet on the window, or when I add a subview to the window. Tried disabling autoresize stuff, and I don't have anything using auto layout. Any thoughts?

Sometimes it's fine and nothing happens, other times it totally breaks my UI and nothing loads

Answer

Mark picture Mark · Feb 4, 2015

It needs to be placed inside a different thread that allows the UI to update as soon as execution of thread function completes:

Modern Swift:

DispatchQueue.main.async {
    // Update UI
}

Older versions of Swift, pre Swift 3.

dispatch_async(dispatch_get_main_queue(){
    // code here
})

Objective-C:

dispatch_async(dispatch_get_main_queue(), ^{
    // code here
});