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?

www.jensolsson.se
  • 3,023
  • 2
  • 35
  • 65

1 Answers1

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.

www.jensolsson.se
  • 3,023
  • 2
  • 35
  • 65
  • Is There a way I can do this in Visual Studio? – user2801184 Jul 26 '18 at 17:31
  • 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](https://stackoverflow.com/q/28302019/2016165) 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]`. – Neph Jun 06 '19 at 15:42
  • Did post it there also now @Neph. – www.jensolsson.se Aug 04 '19 at 10:49
  • Great info, spent hours finding the issue and this pin pointed it for me. Thanks! – Daniel Jul 30 '21 at 00:48