AppBakery
1 min readMar 9, 2021

--

Hi Dirk

Thanks for your comment.

I assume your problem with EnvironmentObject is similar to the one described in Maciej comment?

If that's the case we'll just refer to our answer to his question.

It is a very good question, if it makes sense to expose a ViewModel to the entire app instead of only one specific view.

That's exactly why there is @EnvironmentObject and @ObservedObject and it depends on the use case.

For ViewModels that belong (at least a specific instance of the ViewModel) to one specific View, you can use @ObservedObject on iOS 13+.

For ViewModels that belong to multiple views, you can use @EnvironmentObject. This can indeed be the case, for example if you have a CurrentUserViewModel. The CurrentUserViewModel might be used in a LoginView and in a SettingsView.

We go into more detail in our Views Layer section, where we describe different property wrappers and when to use them.

Cheers

AppBakery

--

--

AppBakery
AppBakery

Written by AppBakery

In-house agency for Mobile Apps & Web at Swiss Federal Railways #sbbcffffs

No responses yet