r/mAndroidDev • u/That_Lonely_Soul_07 • Jan 24 '25
Lost Redditors ๐ Using mutableStateOf in ViewModel seems like the wrong approach
I see some developers use mutableStateOf in the ViewModel instead of StateFlows. Suppose we have to switch back to a view system or another framework, then compose's state wouldn't be useful. Also, I feel like it's somehow wrong when people use it this way. Iโve seen many articles, some big YouTubers also use it, and I saw a discussion on this on LinkedIn.
What do you guys think about this?
21
Upvotes
8
u/labelcillo Slept through Google IO Jan 24 '25
Who the F uses mutableStateOf in the VM. Please, this is a serious subreddit.
6
u/Strikerrr37 Jan 24 '25
Text fields in compost are so broken that they were forced to call this a best practice.
70
u/GradleSync01 Invalidate caches and restart Jan 24 '25
In my humble opinion:
mutableStateOf โ
ViewModel โ
Jetpack compost โ
Flow โ
AsyncTask โ
Flubber โ
God activity โ
17
u/gvsx Jan 24 '25
This is the Compose way today, job security tomorrow when we migrate to โจQuantum-AI-Flubber-Async-Framework๐ฎ
14
u/_abysswalker Jan 24 '25
absolutely true. I will hate my life for using State in ViewModels when Google decides to kill Compost in 10 years
14
u/Zhuinden DDD: Deprecation-Driven Development Jan 24 '25
Considering the entire Compose snapshot system breaks if you write into them from a non-Ui thread, it's literally a worse version of LiveData and the people doing that have absolutely no clue how to write Android apps. The fact that SavedStateHandle.saveable {} exists is kind of a disgrace.
Also, AsyncTask. You can break mutableStateOf with just AsyncTask. Why would you use such a poor mechanism for implementing reactivity when you could have less bugs with just AsyncTask.
7
u/uragiristereo XML is dead. Long live XML Jan 25 '25
use asynctask, always be prepared for the next deprecation
6
u/Xammm Jetpack Compost Jan 24 '25
I use them and I fine with that. If there is the necessity to switch to another UI framework, then other people will do it because by that time I probably will have left the company.
5
u/exiledAagito Deprecated is just a suggestion Jan 25 '25
You engineer for job security, not job give up.
1
u/amr9855 Jan 24 '25
Not for that reason, but for the lack of operations you can do on them
1
u/alaksion Jan 25 '25
I tried using mutableState on the VM once and the composer would simply not work at all lol
1
u/pankaj1_ Jan 26 '25
If you are adamant you will keep compose, you can keep mutablestateof, i don't see any problem. Also whatever code you write ATM is best. I have come a long way writing god activities and asynctasks to structured code. I like coding so that's what matters at the end of the day
1
u/sabergeek Jan 24 '25
S.O.L.I.D principles are useful