What is the likely cause of layout constraints not being applied correctly, resulting in unexpected UI behavior or a crash?

iOS interview question for Intermediate practice.

Answer

UI updates (activating constraints) are happening on a background thread, which is a violation of UIKit's threading rules.

Explanation

Updating UI elements, including activating AutoLayout constraints, must be done on the main thread. Performing these operations on a background thread using GCD, as shown in the code, is a serious bug that can lead to crashes, visual glitches, and other undefined behavior. The NSLayoutConstraint.activate call must be dispatched back to the main queue.

Related Questions