8.4 · onAppear, task, onChange and scenes
SwiftUI has no viewDidLoad. Work attaches to a view’s appearance in the tree and to changes in values, and the app itself is a set of scenes.
Mental model. Tie work to the lifetime of a view (task) or to a value changing (onChange), not to “the screen was created”.
View work
<code>onAppear</code>, <code>task</code> and <code>onChange</code>.
- When do onAppear, task and onChange run, and which one do you use for async work?
onAppear runs when a view is inserted into the view hierarchy that is on screen, and onDisappear when it is removed. It can run more than once for the same…
The app
Scenes and the App protocol.
- What is the App protocol, what are scenes, and what does scenePhase tell you?
A SwiftUI app is a type marked @main that conforms to App and returns a Scene from its body. A scene is a unit the system can show and manage independently…
More in SwiftUI
- 8.1 · What a View is, and how SwiftUI knows which view is which
- 8.2 · The update loop, dependency tracking and the cost of body
- 8.3 · State, Binding, Environment and Observation
- 8.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout
- 8.6 · Lists, ForEach and NavigationStack
- 8.7 · Animations, PhaseAnimator, Canvas and TimelineView
- 8.8 · Preference keys and coordinate spaces
- 8.9 · Previews and the macros behind SwiftUI
- 8.10 · State, bindings, Observation and environment, checked by running them
- 8.11 · The order of events in a render, and what Swift 6 isolation means for views
- 8.12 · Sharing space, alignment guides, adaptive containers and grids, measured
- 8.13 · What a ViewBuilder produces, and saving and restoring a navigation path