Detect invalid use of AppKit, UIKit, and other APIs from a background thread.
- Xcode 9.0+
The Main Thread Checker is a standalone tool for Swift and C languages that detects invalid usage of AppKit, UIKit, and other APIs on a background thread. Updating UI on a thread other than the main thread is a common mistake that can result in missed UI updates, visual defects, data corruptions, and crashes.
How the Main Thread Checker Works
At app launch, the Main Thread Checker dynamically replaces the implementations of methods that should only be called on the main thread with a version that prepends the check. Methods known to be safe for use on background threads are excluded from this check.
The performance impact of the Main Thread Checker is minimal, with a 1–2% CPU overhead and additional process launch time of <0.1 seconds.
Because of its minimal performance overhead, the Main Thread Checker is automatically enabled when you run your app with the Xcode debugger.
Updating UI from a Completion Handler
Long-running tasks such as networking are often executed in the background, and provide a completion handler to signal completion. Attempting to read or update the UI from a completion handler may cause problems.
Dispatch the call to update the label text to the main thread.