How to solve: This application is modifying the autolayout engine from a background thread
Asked Answered
G

1

7

This error is logged to the console when some part of the code is changing UI items from other threads than the main thread. But how can I find where it does this?

Gizzard answered 15/8, 2017 at 8:26 Comment(0)
G
14

Main problem with "This application is modifying the autolayout engine from a background thread" is that it seem to be logged a long time after the actual problem occurs, this can make it very hard to troubleshoot.

I managed to solve the issue by creating three symbolic breakpoints.

Debug > Breakpoints > Create Symbolic Breakpoint...

Breakpoint 1:

  • Symbol: -[UIView setNeedsLayout]

  • Condition: !(BOOL)[NSThread isMainThread]

Breakpoint 2:

  • Symbol: -[UIView layoutIfNeeded]

  • Condition: !(BOOL)[NSThread isMainThread]

Breakpoint 3:

  • Symbol: -[UIView updateConstraintsIfNeeded]

  • Condition: !(BOOL)[NSThread isMainThread]

With these breakpoints, you can easily get a break on the actual line where you incorrectly call UI methods on non-main thread.

Gizzard answered 15/8, 2017 at 8:26 Comment(4)
Is There a way I can do this in Visual Studio?Protest
Thank you so much! My app kept logging 3 different "modifying from a background thread" messages but they didn't actually show up until at least 10 seconds later - after a segue to a different ViewController, even though the code there doesn't even actively use background threads. Would you mind posting your answer (or at least linking to it) here too? It's the question that shows up as the first search result and people also keep linking to it. I wouldn't have found yours if I hadn't googled !(BOOL)[NSThread isMainThread].Tamishatamma
Did post it there also now @Neph.Gizzard
Great info, spent hours finding the issue and this pin pointed it for me. Thanks!Leiva

© 2022 - 2024 — McMap. All rights reserved.