.
Is it possible? 🤔 (I wouldn’t create the viewmodel in
ScreenA
😞)
i
Ian Lake
10/26/2021, 7:27 PM
No, it sounds like your screen b ViewModel should have been wider scoped to begin with (i.e., to a navigation graph that contains at least both B and C)
Ian Lake
10/26/2021, 7:28 PM
But really, a ViewModel should be tightly tied to only display the data relevant to that screen; you probably want a completely separate shared ViewModel rather than co-opting a screen specific one
n
nglauber
10/26/2021, 7:30 PM
I understand… It’s possible to create one viewmodel per screen. But I would have to do an API call twice unnecessarily 🤷♂️
nglauber
10/26/2021, 7:31 PM
Anyways… I’ll let my viewmodel scope more wider
c
Casey Brooks
10/26/2021, 7:50 PM
Another option would be to hold the state of ViewModelB in a single serializable class, which gets passed in serialized form from ScreenB to ScreenC (either JSON passed as a string, or as a Parcelable class).
Alternatively, cache the API call in a local DB, and have both screens read from the DB instead of directly displaying API data. Either class then calls to a "repository" which handles the caching logic internally, and neither the DB nor the API are directly exposed to the VM
i
Ian Lake
10/26/2021, 9:54 PM
Yeah, a shared cache accessed by the two ViewModels (either as part of your repository layer or as a separate thing) seems like a good layering practice in general