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>.

  1. When do onAppear, task and onChange run, and which one do you use for async work? 8.4.1 · L2
    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.

  1. What is the App protocol, what are scenes, and what does scenePhase tell you? 8.4.2 · L3
    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