0Pricing
Swift Academy · Lesson

Main Thread Checker and Hangs

Detecting UI work on background threads and resolving main-thread hangs.

Main Thread Checker

The Main Thread Checker (MTC) is a Xcode diagnostic that reports UIKit/AppKit API calls made from background threads at runtime.

// Enabled by default in Xcode Debug scheme:
// Product → Scheme → Edit → Diagnostics → ☑ Main Thread Checker
// Violating calls log a purple runtime warning in Xcode

Common MTC Violations

Updating UI from completion handlers or background queues are the most frequent violations.

URLSession.shared.dataTask(with: url) { data, _, _ in
  self.label.text = "Done"  // VIOLATION: background thread
}.resume()
// Fix:
DispatchQueue.main.async { self.label.text = "Done" }

All lessons in this course

  1. Instruments Basics: Time Profiler
  2. Allocations and Leaks Instruments
  3. Main Thread Checker and Hangs
  4. Optimising Hot Paths: COW, Inlining and Specialisation
← Back to Swift Academy