https://kotlinlang.org logo
#rx
Title
j

jw

10/23/2018, 3:57 AM
i recommend not using viewmodels (the library), but for the general concept of view models you should be posting back to the main thread before they are cached
g

gsala

10/23/2018, 7:08 AM
i recommend not using viewmodels (the library)
I'm very interested on knowing the reason behind this opinion
j

jw

10/23/2018, 1:31 PM
It's just a service locator that survives rotation
But not even that great of one. And its now infected the base types in AndroidX so it gets dragged into your APK whether you want it or not
g

gsala

10/23/2018, 2:36 PM
I see. Then what would an alternative be? I see the VMs as a way to keep alive everything that should be alive during the natural use of an app, basically relying on the
onCleared
call to tell me the app is not in use anymore. How else can I get to that point? If I go back to pre-VM times, I can only remember having to deal with presistance, services and loaders/headless fragments. That or resetting everything every time the screen is rotated (or another config change)
j

jw

10/23/2018, 3:07 PM
Move the entirety of state outside the activity and the only responsibility of things in the activity is to bind UI models. Activities are a porthole to state.
g

gsala

10/23/2018, 3:08 PM
that's exactly what my activities/fragments are right now. But ViewModels are holding that state and mutating it
what other way do I have to keep that state out of the activity?
j

jw

10/23/2018, 3:10 PM
in reverse order of lifetimes: the activity non-config instance, the application instance, the process lifetime (i.e., static scope)
g

gsala

10/23/2018, 3:18 PM
do you have an example of how you keep the state in android in your Sdk Search project or is it different due to multiplatform (no need to link it, just need to know if I can find something there that might be interesting)
j

jw

10/23/2018, 3:19 PM
It uses the activity non-config instance to retain the presenter through configuration changes. There's only one screen, though, so it can get away with this. I keep meaning to figure out a way to make the app more complex so I can design something more robust but just haven't had the time.
g

gsala

10/23/2018, 3:30 PM
Cool. I'll take a look. Thanks 👍
4 Views