View Lifecycle

The interviewer asks

“What's the view lifecycle in SwiftUI? How does it differ from UIKit?”

How to answer

A SwiftUI View is a cheap value, not an object with a lifecycle: its init and body run whenever SwiftUI needs a fresh description, which can be many times. The real lifecycle belongs to the view's identity in the render tree: it appears (onAppear, .task starts), updates when data it read changes (body re-runs, onChange fires), and disappears (onDisappear, .task cancelled). UIKit is the opposite: a UIViewController is a long-lived object with explicit callbacks (viewDidLoad, viewWillAppear, viewIsAppearing, viewDidAppear, viewWillDisappear), and since iOS 26 it can also re-render automatically through updateProperties() and Observation tracking.

Key points

  • init: runs on every parent update; keep it trivial, no side effects
  • body: runs when observed state changes; must be pure and fast
  • .task { }: the place for async loading; starts before the view appears, is cancelled when it disappears; .task(id:) restarts when a value changes
  • .onAppear / .onDisappear: synchronous hooks; can fire more than once (tab switches, navigation pop, lazy rows scrolling)
  • .onChange(of:) reacts to value changes; @Environment(\.scenePhase) for foreground/background
  • Rough UIKit mapping: viewDidLoad ≈ first .task/onAppear (guard with a flag or load in the model), viewWillAppear/viewIsAppearing ≈ onAppear, viewDidDisappear ≈ onDisappear

If they dig deeper

State lifetime follows identity: @State (and an @Observable model owned with @State) lives as long as the view's identity, and building with Xcode 27 the @State default value is created only once, the first time SwiftUI instantiates the view. Lazy containers create rows on demand, so row onAppear/.task run as you scroll. In UIKit, viewIsAppearing is the right place for geometry- and trait-dependent setup, and with the iOS 26 SDK reading @Observable properties in updateProperties() or layoutSubviews invalidates automatically. App and scene lifecycle are separate: with the iOS 27 SDK UIKit apps must adopt the UIScene life cycle (SwiftUI App apps already do), and scenePhase is the SwiftUI way to observe it.

Common pitfalls

Assuming init or onAppear runs once ("viewDidLoad"). Starting Task { } in onAppear (not cancelled on disappear; use .task). Side effects or heavy work in body. Reloading data on every onAppear when the user just pops back. Creating an expensive object as a plain let in a view (recreated on every init) instead of owning it with @State or injecting it.

How to use it

swift
@MainActor
@Observable
final class ProfileModel {
    var name = ""
    private(set) var hasLoaded = false

    func loadIfNeeded() async {
        guard !hasLoaded else { return }      // viewDidLoad-style "once"
        // name = try await api.fetchProfile().name
        hasLoaded = true
    }
}

struct ProfileView: View {
    @State private var model = ProfileModel()  // lives as long as this view's identity
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        Text(model.name)
            .task { await model.loadIfNeeded() }          // async, cancelled on disappear
            .onAppear { print("appeared") }               // sync, may run many times
            .onDisappear { print("disappeared") }
            .onChange(of: scenePhase) { _, phase in
                if phase == .background { print("save draft") }
            }
    }
}

Best practices

  • Load async data in .task (or .task(id:)), never onAppear { Task { } }
  • Keep init and body free of side effects; they run far more often than the view "appears"
  • Put "load once" logic in the model, owned with @State, rather than relying on appearance callbacks
  • Use scenePhase for app foreground/background, not view appearance

Related concepts

  • .task modifier, View identity, body as a computed property, @State / @Observable, UIKit view controller lifecycle (viewIsAppearing, updateProperties()), UIScene life cycle