Skip to content

Modern SwiftUI: Standups App #140

Closed
Closed

Description

@BryanJBryce

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?

Activity

  1. mbrandonw commented on May 23, 2023

    @mbrandonw
    Member

    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.

  2. locked and limited conversation to collaborators on May 23, 2023
  3. converted this issue into a discussion #141 on May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions