pointfreeco / episode-code-samples

💾 Point-Free episode code.
https://www.pointfree.co
MIT License
939 stars 289 forks source link

Modern SwiftUI: Standups App #140

Closed BryanJBryce closed 1 year ago

BryanJBryce commented 1 year ago

StandupsList stores its model in an ObservedObjectthere isn't a StateObject further up the line, the model is just passed in as a parameter into the View's init. From what I understand if nothing retains the ObservableObject a new instance is created every time that view redraws its body. Was this intended behavior or left as an exercise to the reader?

mbrandonw commented 1 year ago

Hi @BryanJBryce, the StandupsList view holds onto its observable object like so:

struct StandupsList: View {
  @ObservedObject var model: StandupsListModel
  …
}

…which makes the parent responsible for providing the model. So it will not be recreated unless the parent decides to recreate it.

And the parent is the app entry point:

@main
struct StandupsApp: App {
  var body: some Scene {
    WindowGroup {
      StandupsList(
        model: StandupsListModel()
      )
    }
  }
}

And that shows that only a single model is created for the entire duration of the app.

Also I am going to move this to a discussion since it isn't really an issue with the code sample.