Michael Long
Apr 14, 2024

--

@Bindable var model: ContentViewModel = ContentViewModel(...)

Please, no. Bindable doesn't manage state and as such this is pretty much the same as doing...

@ObservedObject var model: ContentViewModel = ContentViewModel(...)

An instance of model will be recreated each and every time the view is recreated for reevaluation.

Article also fails to discuss another large elephant in the room, in that unlike @StateObject, anything fed to @State is not thunked, which means that an instance of your Observable view model will be recreated each and every time the view is recreated for reevaluation.

State will do the right thing and only use the first instance. but a new model WILL be recreated.

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)