Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]

It’s hard to find the UI code which is not executed in main thread sometimes. You can use the trick below to locate it and fix it.

  1. Choose Edit Scheme -> Diagnostics, tick Main Thread Checker.

    Xcode 11.4.1

    Click the small arrow next to the Main Thread Checker to create a Main Thread Checker breakpoint.
    enter image description here

    Previous Xcode

    Tick on Pause on issues.
    enter image description here

  2. Run your iOS application to reproduce this issue. (Xcode should pause on the first issue.)
    enter image description here

  3. Wrap the code that modify the UI in DispatchQueue.main.async {}
    enter image description here

Leave a Comment