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/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.
2
u/ClaymoresInTheCloset Jun 21 '22
Definitely not on the battery either, that's more what I'd care about
7
u/saslykas Jun 21 '22
When it comes to separation of UI logic, I personally use what I call MVC architectural pattern. It’s better than ViewModel-based MVVM because it incorporates the most important architectural insight in Android world – that Activities and Fragments shouldn’t contain UI logic at all.
It is the only reason? What if fragment does not contain any UI logic, but still uses ViewModel? (custom views, compose)
In fact, you’ll probably want to rely on this mechanism even if you do use ViewModel (e.g. it would be extremely dirty to store input fields state in ViewModel). So, no benefit for ViewModel in the context of UI state.
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 opinion, non-cancellable GlobalScope would be much better default Coroutine Scope to use inside ViewModels. So, the “clever” management of viewModelScope is actually a foot-gun (time-bomb), not a benefit.
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.
3
u/saslykas Jun 22 '22
Basically, he says that you should never pass viewModel instance to the composable, to which I strongly agree. The only exception is a pure compose app (which is not happening any time soon) VM should not be used.
20
Jun 21 '22
[removed] — view removed comment
12
u/Zhuinden EpicPandaForce @ SO Jun 21 '22
This is true on paper, although there's some "cult-like" notion that "if a library comes from Google, then it HAS to be good, and therefore you HAVE to use it otherwise you're a bad developer writing bad code and you'll doom every future maintainer to a slow and cold death" 🤔
But they always conveniently ignore (or just say "yeah but that was then and this is now so that basically never happened) the list of deprecated and abandoned approaches from AsyncTaskLoader to Agera, each being solved as the next savior of Android development and software design.
No, Google isn't infallible. In fact, their primary interest is to make something that works for 3 years, and then kill it off with something new. Dagger living so long only happened because Google actually uses their library internally, possibly on multiple projects and not just one.
4
u/FrezoreR Jun 22 '22
One problem here is that people conflate and confuse Androids ViewModel library with the MVVM pattern.
I think you're a bit cynical. There might be people that like the libraries just because they are from Google but there are also people like you that always dislike everything coming out of Google.
The truth is somewhere in-between.
That being said there's no doubt that MVVM is a very flexible pattern for your apps presentation architecture. Also, you don't need to use Android ViewModel, since it's an architectural pattern and not a library.
4
u/Zhuinden EpicPandaForce @ SO Jun 22 '22
Also, you don't need to use Android ViewModel, since it's an architectural pattern and not a library.
It's actually the opposite, Android ViewModel is a library, and not an architectural pattern. It was said so back 5 years ago and it still stands. It's a way to survive config changes and that's it.
3
u/FrezoreR Jun 22 '22
You're reading that sentence wrong. MVVM is a pattern not a library. Android ViewModel is a library you can use, but as stated it is not needed for MVVM.
That being said an Android ViewModel is nothing more than a container that survives life cycle changes, which is a nice property to have for many apps.
1
u/Zhuinden EpicPandaForce @ SO Jun 22 '22
If that's what you meant, then I really am reading the sentence wrong :D
1
u/FrezoreR Jun 22 '22
Hehe yeah, I can see how you read it the other way so a clarification was warranted :D
2
u/FrezoreR Jun 22 '22
That's the fun thing with programming. You can continuously learn, and what is the best way to do something depends a lot on the circumstances.
7
u/yaaaaayPancakes Jun 21 '22
Totally agree. Though one thought on:
there's some "cult-like" notion that "if a library comes from Google, then it HAS to be good, and therefore you HAVE to use it otherwise you're a bad developer writing bad code and you'll doom every future maintainer to a slow and cold death"
This is just the modern version of the old adage "No one ever gets fired for buying IBM". Corporate culture doesn't generally reward sticking your neck out. Higher ups that aren't familiar with the shittiness of Google API's just see the name Google attached and go "well that seems reasonable".
It's unfortunate, but it's the way the world works.
8
u/Zhuinden EpicPandaForce @ SO Jun 21 '22
This is just the modern version of the old adage "No one ever gets fired for buying IBM". Corporate culture doesn't generally reward sticking your neck out. Higher ups that aren't familiar with the shittiness of Google API's just see the name Google attached and go "well that seems reasonable".
It's unfortunate, but it's the way the world works.
They hire Android developers to ship the app that behaves according to the specifications. If they're micromanaging implementation decisions beyond that without them being experts in Android, then it's really a case of bad management.
I've been hired to develop a "hybrid web-based app" that we implemented with WebView and jQuery Mobile, they did specify technology at that time - it was fun to rewrite the next year native.
But at least that's technological decisions, not "what library are you using". They'd then complain if you use core SDK APIs, and they'd complain if you don't. Create enough deadlocks and you'll just never get a working app.
1
u/yaaaaayPancakes Jun 21 '22 edited Jun 21 '22
I agree that it's bad management, but that's the reality of most places, given the Peter Principle.
I mean, I've had a hard time selling your Simple Stack library over Jetpack Nav, strictly because management heard of Google, but never heard of you. Didn't help that I had a fully functional app in hand demonstrating how nice Simple Stack is.
2
u/Zhuinden EpicPandaForce @ SO Jun 22 '22
Fun fact btw is that i've seen a project fail before because of AngularJS's performance problems :D
1
u/Zhuinden EpicPandaForce @ SO Jun 21 '22 edited Jun 21 '22
Didn't help that I had a fully functional app in hand demonstrating how nice Simple Stack is.
Not much I can do about that. Bad management micromanaging things they don't understand is just the way it is sometimes.
Hypothetically you can always write your own fragment navigation. Then they don't look at it and say, "oh no, an external dependency". Not realizing that every software developer is an external dependency, most developers leave after at most 3 years, and tooling with additional inherent complexity will always hold additional inherent complexity.
But if you ever figure out how to compete with "Google is trustworthy because they are a MAANG company", feel free to tell me. Maybe then you won't have to ditch Simple-Stack for solutions with higher usage complexity.
1
3
u/anemomylos 🛡️ Jun 21 '22
finalizemethod ofObjectshould do that, right?
ps i am gonna create a unpopularopinion flair just for you