read
We all know UI calls have to be run in the main thread.
But sometimes mistake happen, and you will see this in debug log:
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
There is a easy way to trace which part of your code is casuing the problem.
- Add Symbolic Breakpoint
- Symbol:
[UIView layoutIfNeeded]
- Condition:
!(BOOL)[NSThread isMainThread]
There are other “symbols” to catch. Create a symbolic breakpoint for each of these symbols:
[UIView updateConstraintsIfNeeded]
[UIView setNeedsUpdateConstraints]
[UIView setNeedsDisplay]
[UIView setNeedsLayout]
Run the app, and Xcode will break at the offending code.
Lastly, the fix as we all know:
DispatchQueue.main.async {
// The UI call
}