Group Group Group Group Group Group Group Group Group

Chapter 15; @ObservedObject var model: ReaderViewModel

In the book (version 1.0.3) the author writes on page 280 of the PDF:

@ObservedObject var model: ReaderViewModel

You don’t inject the model when initializing the view anymore. Instead, you bind the model so that, any time its state changes, your view will receive the latest data and generate its new UI “snapshot”.
The @ObservedObject wrapper does the following:

  1. Removes the property storage from the view and uses a binding to the original
    model instead. In other words, it doesn’t duplicate the data.
  2. Marks the property as external storage. In other words, it denotes that the piece
    of data is not owned by the view.

However, in class SceneDelegate, func scene( …)
isn’t the chapter 15 “final” code injecting the model when initializing the view, with this, below?

let viewModel = ReaderViewModel()

let rootView = ReaderView(model: viewModel)
.environmentObject(userSettings)

If we only want to use “a binding to the original model”, shouldn’t viewModel be a data member of some class and then we pass a reference to that data member to ReaderView?

If the chapter 15 “final” code is correct, what is retaining the reference to the viewModel? Because @ObservedObject “removes the property storage from the view” ?

@freak4pc Can you please help with this when you get a chance? Thank you - much appreciated! :]

I believe @scotteg will be able to offer more insights here.