-
I'm building a new app and wanted to embrace the new When setting up my I noticed that the bug only occurred when I referenced any property on self within the Does the new |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @timbueno, yes this is to be expected unfortunately, and is a big gotcha with In your Probably the safest way to prevent this is to make it a point to never access the observable computed properties in an initializer, and instead only access their underscored version: private func bind() {
_ = _appState
} That will work fine. You can also just make sure to not create the home model in the body of the app entry point: static let model = HomeViewModel()
var body: some Scene {
WindowGroup {
HomeView(viewModel: Self.model)
}
} That works for the entry point of the app, but you could theoretically still run into trouble in another part of the app. |
Beta Was this translation helpful? Give feedback.
Hi @timbueno, yes this is to be expected unfortunately, and is a big gotcha with
@Observation
and SwiftUI, and has nothing to do with this library or our "modern SwiftUI" techniques. We even tweeted about it here and discussed in this episode.In your
HomeViewModel
initializer you are accessingself.appState
(viabind
), and so that tells the view (in this caseScootApp
) that it should re-render when that property changes. And so when it does change, that causesScootApp
to re-compute itsbody
, and then you are generating a new model.Probably the safest way to prevent this is to make it a point to never access the observable computed properties in an initializer, and instead only access t…