Minimizing Body Re-evaluation
Reduce unnecessary view recomputation.
Minimizing Body Re-evaluation is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Body Is Recomputed Often
SwiftUI calls a view’s body whenever its inputs change. A heavy or over-connected body recomputes too frequently, hurting performance.
What Triggers Re-Evaluation
A body re-runs when any @State, @Binding, observed object, or environment value it reads changes—or when its parent passes new values.
struct Row: View {
let title: String // changes -> body re-runs
var body: some View { Text(title) }
}Narrow What You Observe
If a view reads a whole large model, any change to that model re-runs its body. Pass only the specific values the view needs.
// Instead of passing the whole model:
struct Price: View {
let amount: Decimal // narrow input
var body: some View { Text(amount, format: .currency(code: "USD")) }
}Split Large Views
Break a giant body into small subviews. SwiftUI re-evaluates only the subviews whose inputs actually changed, not the whole screen.
struct Screen: View {
var body: some View {
VStack {
Header() // re-runs only if its inputs change
ItemList()
Footer()
}
}
}Observable and Field-Level Tracking
With the @Observable macro, SwiftUI tracks which exact properties a view reads. A view that reads only model.name ignores changes to model.count.
@Observable final class Model {
var name = ""
var count = 0
}
// A view reading only name won’t re-run when count changesAvoid Work in body
The body should describe the UI cheaply. Heavy computation (sorting, filtering) belongs in the model or a cached property, not recomputed on every body call.
// Bad: sort inside body (runs every recompute)
// Good: sort once in the model, expose sorted arrayStable Closures and Inputs
Passing a freshly built array or closure each render can defeat SwiftUI’s equality checks. Hoist stable values so inputs compare equal across recomputes.
// Hoist constants out of body
private let columns = [GridItem(.flexible())]Beware Expensive Initializers
A view’s init may run many times. Avoid heavy work there; SwiftUI creates view values frequently even when it does not render them.
struct Card: View {
init(_ item: Item) { /* keep this trivial */ }
var body: some View { Text("Card") }
}Environment Reads Cost Updates
Reading an environment value subscribes the view to it. Read environment values only where needed, in the smallest subview, to limit re-evaluation scope.
@Environment(\.colorScheme) private var schemeLazy Containers
Use LazyVStack/LazyHStack and List so off-screen rows are not built until needed, cutting body calls for large collections.
ScrollView {
LazyVStack {
ForEach(items) { RowView(item: $0) }
}
}Measure, Then Optimize
Do not guess which body is hot. Use the SwiftUI Instrument to find views with high body-evaluation counts, then apply these techniques there.
Quick Check: Re-Evaluation
Test your understanding of body re-evaluation.
Recap: Minimizing Body Re-Evaluation
Bodies re-run when their inputs change, so pass narrow values, split large views into small subviews, and lean on @Observable field-level tracking. Keep bodies and initializers cheap, hoist stable inputs, read environment values minimally, and use lazy containers for big collections.
Always confirm hot spots with the SwiftUI Instrument before optimizing.
Frequently asked questions
Is the “Minimizing Body Re-evaluation” lesson free?
Yes — the full text of “Minimizing Body Re-evaluation” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Minimizing Body Re-evaluation”?
Reduce unnecessary view recomputation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Minimizing Body Re-evaluation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding View Identity
- Minimizing Body Re-evaluation
- Equatable Views and @State Scope
- Profiling with the SwiftUI Instrument