How to correctly initialize a view model in SwiftUI: https://chris.eidhof.nl/post/swiftui-view-model/ (for when the view needs ownership of the view model).
SwiftUI View Model Ownership — Chris Eidhof

@chris In the fixed example, is it correct to say that an instance of the view model object will be needlessly created each time the view’s initializer is called by the parent? As in: when you change the selected room in the parent, the child’s initializer is run, name is set to the new name, the view model object is created and then not used (because of the attribute graph as you mentioned), and then the “original” view model is replaced by a third inside of onChanged?
@kyle @chris yes, that is correct
@shadowfacts @kyle yes! Good catch. I think that should be fixed, don't know the proper solution yet... I'll have a think.
@shadowfacts @kyle I added a note to the post just now (should be live in a minute). Thank you!

@chris @shadowfacts @kyle echoing the above, I think this is made more explicit if you use the State init, like _vm = State(initialValue: …)

This makes it more obvious why the initial example was broken and avoids a new vm on every init (but you still need your onchange)

@bens @chris @kyle unless I'm misunderstanding, that does not avoid the new view model on every init. State(initialValue:) would have to take an autoclosure for that to be the case

https://developer.apple.com/documentation/swiftui/state/init(initialvalue:)
init(initialValue:) | Apple Developer Documentation

Creates a state property that stores an initial value.

Apple Developer Documentation
@shadowfacts @chris @kyle StateObject did, I assumed this did as well
@bens @chris @kyle indeed not. there was a very brief window during one of the betas (after observable was introduced, iirc) where there was an autoclosure State init, but it disappeared

@shadowfacts @chris @kyle woof -- here's to hoping there's some magical new tool that eliminates all this confusion.

IME it’s far too easy to just not realize you're accidentally recreating state 60 times/sec because you modeled it incorrectly.

@chris @shadowfacts You’re doing yeoman’s work, thank you. My ulterior motive for confirming that detail is I that I am coming to the conclusion—and want to learn how to communicate—that this pattern of view models are implicitly discouraged by Apple and SwiftUI, based on the worsening ergonomics of using them.
@kyle @chris I largely agree and don't really use view models myself, but I think it's fine (maybe not great, but fine) to do so without worrying about the extra initializations unless you have concrete/measurable reasons to

(I have written about related things before: https://shadowfacts.net/2024/swiftui-lifecycle/)
Your View's Lifetime Is Not Yours

The outer part of a shadow is called the penumbra.

Shadowfacts

@shadowfacts @kyle I updated the article to show a variant that doesn't always recreate the VM. It's tricky.

BTW, @shadowfacts I really enjoyed reading your article about preferences!

@kyle @shadowfacts yeah I don't hold strong opinions there, but people *really* want to initialize in init. I wrote the post more so that when I see that happening, I have a "short" explanation that is linkable :).