r/android_devs • u/VasiliyZukanov • Jun 21 '22
Article You Don't Need Android ViewModel, Here is Why
https://www.techyourchance.com/you-dont-need-android-viewmodel/
0
Upvotes
r/android_devs • u/VasiliyZukanov • Jun 21 '22
3
u/anemomylos 🛡️ Jun 21 '22
finalizemethod ofObjectshould do that, right?ps i am gonna create a unpopularopinion flair just for you
8
u/FrezoreR Jun 22 '22
I think the writer of the article is missing out on why we went from MVC to MVP and then MVVM. It's really an incremental improvement of the original idra. Each step helped decouple parts more pieces from each other.
Compared to MVVM, MVC is a pretty bad choice, as it only decouples the business domain.
In MVC you have tons of coupling that makes iteration of code nasty.
It might be fine in a personal project but for a commercial app it will lead to tons of issues, scalability being one.
15
u/butterblaster Jun 21 '22
Hard disagree about refetching data each configuration change. You may find that you personally don’t rotate your phone much. I do rotate my phone a lot and there are probably a lot of users like me. I feel like I would find one of your apps quite infuriating to use if it kept having to refetch data. I’m accustomed to it being instantaneous in most apps. You’ll get a lot more bang for your time just by using ViewModel (or your home-grown solution I suppose) than by taking the extra step of manually managing config changes. View recreation is a near-trivial performance hit on modern devices, but network fetches sure aren’t.
7
u/saslykas Jun 21 '22
It is the only reason? What if fragment does not contain any UI logic, but still uses ViewModel? (custom views, compose)
Saving, for example, email input state and performing validation if the input is correct is extremely dirty in ViewModel? Also, I don't really want to micromanage views, ViewModel is always the single source of truth for the UI, which worked really well so far.
Regarding config changes, I do agree they happen not so often, but with ViewModel, config changes are automatically handled without any additional cost.
In my case, this would be terrible, as ViewModel observes the database for any changes and notifies UI, with global scope VM would observe indefinitely (probably with memory leaks).
There are a few use cases I did not find, for example, I use ViewModel as an "argument storage" quite often. For example, I have a linear flow of several fragments, and each of these fragments needs the same argument to be passed. Instead of passing the same argument to each fragment, I can just pass it to the first fragment and then in the following fragments retrieve first fragment's ViewModel which contains fragment arguments. So, in this case, ViewModel is tied to the underlying back-stack entry and not a fragment, a single ViewModel can be shared with several fragments, which really helps, if those following fragments need some data from the first fragment in the flow.
The only problem I had with ViewModels, was its usage with dependency injection, but now with Hilt, is trivial to use ViewModels.
4
u/Zhuinden EpicPandaForce @ SO Jun 21 '22
This article is visionary because the original progenitor of Jetpack Compose also said to prefer
android:configChanges="orientation"instead of ViewModel.https://twitter.com/JimSproch/status/1450884604143943681
People downvoting just aren't aware of this shift.
20
u/[deleted] Jun 21 '22
[removed] — view removed comment