In short
- A hang is a noticeable delay between a tap or keystroke and the screen responding. It is almost always the main thread being busy with work, or blocked waiting on something, for too long.
- Apple's tools start reporting when the main run loop is unresponsive for more than 250 ms. Catch hangs early with Thread Performance Checker, on-device Hang Detection and the Hangs instrument; watch the hang rate and hang reports in Xcode Organizer or MetricKit once the app ships.
- Most fixes come down to one rule: the main thread updates the UI and nothing else. File I/O, networking, decoding, waits on semaphores and heavy synchronous calls belong somewhere else.
What counts as a hang
Apple separates two kinds of unresponsiveness. A hang is a delay in a discrete interaction: a button press, a keystroke, a tap that should open a screen. A hitch is a break in continuous motion: a scroll or animation that sticks and then jumps ahead. The two share a cause (too much work on the main thread) but not a threshold.
| Hang | Hitch | |
|---|---|---|
| What the user sees | The app doesn't react to a tap or key for a moment, or freezes | Scrolling or an animation stutters |
| When it becomes noticeable | Delays under 100 ms are rarely noticed; a few hundred ms feels unresponsive | A frame that misses a single refresh: 8.3 ms at 120 Hz, 16.7 ms at 60 Hz |
| When Apple's tools report it | Most report the main run loop busy for more than 250 ms; the Hangs instrument lets you pick a lower threshold | Only when a screen update is actually late |
Because a long main-thread stall also drops frames, Apple's advice is to fix hangs first: they are easier to understand, and fixing them usually removes big sources of hitches too. Scroll hitches have their own causes, covered in Reducing Hitches in SwiftUI List and LazyVStack Scrolling.
At the extreme end, the system's watchdog terminates an app that blocks its main thread for too long. Those crash reports carry the termination code 0x8badf00d. A Termination Description containing scene-create means the app didn't draw its first frame in time at launch; scene-update means it stopped updating its UI because the main thread was too busy. An app that disappears with no crash report of its own may instead have been terminated for using too much memory: see Jetsam Events on iOS.
Busy or blocked: the first question to answer
Hang reporting watches the main run loop. A healthy run loop wakes for an event, handles it quickly and goes back to sleep. Anything that keeps it awake (an event handler, a timer callback, work sent to the main queue or the main actor) delays every event queued behind it. When you find a hang, the next question is which of two things the main thread was doing:
| Busy main thread | Blocked main thread | |
|---|---|---|
| What's happening | The thread is running your code (or framework code you called) for too long | The thread is waiting: on a lock, a semaphore, a file read, a network reply, another process |
| What a trace shows | High CPU on the main thread during the hang | Little CPU on the main thread during the hang |
| Typical fix | Do less, or move the work off the main thread and update the UI when it's done | Remove the wait: use the asynchronous API, or make sure nothing on the main thread waits on background work |
Instruments Time Profiler for Swift Concurrency Bottlenecks walks through reading a trace for exactly this split.
Tools that catch hangs, from development to the field
| Tool | When it helps | What to know |
|---|---|---|
| Thread Performance Checker | Every run from Xcode | On by default for the Run action. Flags priority inversions and non-UI work on the main thread as runtime issues; a test plan can turn them into test failures. |
| On-device Hang Detection | Using a development or TestFlight build day to day | Settings > Developer > Hang Detection. The device notifies you when a hang happens and keeps a report you can open in Instruments on your Mac. |
| Hangs instrument | Reproducing and analysing a specific hang | Included in the Time Profiler, CPU Profiler and Hitches templates. Needs Instruments 14 or later and iOS 16 or later. Lets you set a reporting threshold below 250 ms. |
| Xcode Organizer: Hangs | After release, across real users | Hang rate in seconds of unresponsiveness per hour, counting only stalls over 250 ms, shown as the median and the 90th percentile and filterable by device. Hang reports group main-thread stack traces sampled when a stall lasts 1 s or more, from users who share data with developers. |
| MetricKit | After release, in your own pipeline | Daily metric reports plus hang diagnostics with call stacks. Before iOS 27: MXMetricManager, MXAppResponsivenessMetric.histogrammedApplicationHangTime and MXHangDiagnostic (iOS 14 and later). From iOS 27: MetricManager, HangTimeMetric and HangDiagnostic; the MX types are marked deprecated in newer SDKs. |
Organizer's hang reports only cover iOS and iPadOS, and how many you get depends on your audience and how many users share analytics. Check the Organizer and MetricKit documentation for the platforms and OS versions your app supports.
A workflow for finding and fixing a hang
- Start from field data. Open the Hangs pane in Xcode Organizer (or your MetricKit dashboard) and find the reports that account for the most hang time in the current release.
- Narrow it down. Filter by device and app version, and find the first version where the problem appears; the changes since the previous version are where to look.
- Reproduce it. Turn on Hang Detection on a test device, or record the flow in Instruments with the Time Profiler template. Use an older device your app still supports: code that is fast on new hardware can hang on old.
- Decide busy or blocked. Check main-thread CPU during the hang in the trace, then follow the stack in the hang report or the Hangs track to the call that took the time.
- Fix it, then guard it. For code that has to stay on the main thread, add an XCTest performance test around it with
measure(_:)and a baseline. Apple suggests 100 ms as the ceiling for a discrete interaction and around 5 ms for code that runs during scrolling, such as data source methods. - Confirm in the field. Compare the hang rate and the report's 14-day trend in Organizer after the fixed version ships.
Field metrics tell you that a release got worse; they rarely tell you why. Instrumentation and Observability Stack for Production iOS Apps covers the logging and signposts that make a hang report easier to place.
Common causes, and what to do instead
| Cause | Do this instead |
|---|---|
| Reading or writing files synchronously on the main thread | Do the I/O on a background queue or task and hop back to the main actor with the result. Prefer async variants of file and data APIs. |
Synchronous networking, including hidden cases such as Data(contentsOf:) or XMLParser(contentsOf:) with an https URL, SCNetworkReachability calls, or BSD DNS functions like gethostbyname | Use URLSession's asynchronous APIs, and NWPathMonitor for reachability. These hangs often only show up on slow networks, so test with Network Link Conditioner. |
Waiting on background work: DispatchSemaphore.wait(), DispatchGroup.wait(), sync onto a busy queue, joining a thread | Don't make the main thread wait. Have the background work call back (or await it) and update the UI when it finishes. Waits like these also cause priority inversions, which Thread Performance Checker flags. |
| Heavy work in the event handler: decoding a large JSON file, image processing, a big sort or filter | Prepare data in the background; use the main thread only to apply the result to views. |
Slow framework calls on the main thread, such as AVCaptureSession.startRunning(), synchronous Vision requests, a large Core Data lightweight migration, or loading 3D models synchronously | Call them from a background queue, or use the framework's async variant where one exists. |
| Swift concurrency that looks off the main thread but isn't (see below) | Make heavy work async and nonisolated, mark it @concurrent where needed, or run it in a detached task. |
The Swift concurrency trap
Wrapping work in Task { } doesn't move it off the main thread. A task inherits the actor of the context that creates it, and a SwiftUI body, a view controller and most UI code run on the main actor. So a synchronous, expensive call inside that task still runs on the main thread, a moment later:
// Still hangs: the Task inherits the main actor,
// and the heavy call is synchronous.
Button("Export") {
Task {
let file = buildExport() // synchronous, slow
show(file)
}
}Apple documents two ways out. Make the heavy function async and nonisolated so it runs on the concurrency thread pool while the task waits, then returns to the main actor to update the UI. Or, when the function can't be made async, run it in Task.detached, which doesn't inherit the actor; give it an explicit priority, because a detached task doesn't inherit one.
@concurrent
func buildExport() async -> URL { /* slow work */ }
Button("Export") {
Task {
let file = await buildExport() // off the main actor
show(file) // back on the main actor
}
}Check which Swift language mode and upcoming features your target uses. With Swift 6.2's NonisolatedNonsendingByDefault feature turned on (part of Xcode's Approachable Concurrency setting), a plain nonisolated async function runs on the caller's actor; @concurrent is what sends it to the thread pool. Swift Concurrency Actor Isolation in Production iOS Codebases covers isolation decisions in more depth.
Hangs at launch
Launch is where main-thread work is most expensive: everything that runs before the first frame delays it, and taking too long gets the app killed with 0x8badf00d and scene-create in the report. The watchdog's stack trace shows what the app was doing when it was killed, not necessarily what used up the time, so read every thread's backtrace to work out how far launch got. Defer anything the first screen doesn't need. App Launch Time Optimization Below 400ms on iOS goes through launch work step by step.