@BindableObject vs @Environment

I need to use MVVM architecture in my app.How to use my view model ... i mean ...with @BindableObject or @Environment

Replies

You'll most likely want to use @ObservedObject (the new name for @BindableObject as of about August) for this, which will let you trigger UI updates whenever your view model changes (or more accurately, whenever an @Published property of your ViewModel changes). However, be aware that the complexity of view updates increases based on the number of published properties within your model. See <https://forums.developer.apple.com/thread/125592> for some profiling I did on this.


If your view model is used in a reasonably limited scope, you may get some performance benefits from either using a struct implementation of the model and an @State or @Binding wrapper rather than a class (any modification to the struct's content will trigger a refresh). Also, don't forget that a View type in SwiftUI is really a 'view description' or a 'data -> UI transformer', so it's entirely appropriate to consider your SwiftUI View to be the same thing as your ViewModel—it just describes how to present state onscreen.