DevPrep

All topics › SwiftUI

How does SwiftUI decide what to redraw? What are identity and diffing?

Senior PRO

The gist

SwiftUI does not store your structs; it stores an attribute graph — a tree of nodes with state and dependencies. When a dependency changes (@State, a property read from an @Observable model, @Environment), only the dependent nodes are marked, their body is re-evaluated, the new tree is compared with the old one (diffing), and only the real changes are pushed to UIKit/Core Animation. To match old and new nodes the framework needs identity.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store