I am done with ViewModels...

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024
  • In this video, I am going to rant about ViewModels and try to explain why I personally do not like them. Obviously, I am very much over exaggerating.

ความคิดเห็น • 34

  • @RackaApps
    @RackaApps 7 วันที่ผ่านมา +7

    I'm still confused a bit. The problems that you mentioned with ViewModel seem to be related to how people structure the ViewModel and not inherently the androidx ViewModel class itself. So I'm guessing you don't like the MVVM structure and not the android VM class itself?
    I personally use the androidx ViewModel class but my states and events resemble MVI with a bit of what you did.
    I do agree though, your example looks cleaner. Will try it out on some personal projects and see how it scales

  • @androiddevo
    @androiddevo 6 วันที่ผ่านมา +3

    I think I am done with Android 😂

    • @s.sellmair
      @s.sellmair  6 วันที่ผ่านมา +3

      @@androiddevo User name says no

  • @manticomar1146
    @manticomar1146 วันที่ผ่านมา

    Just use MVI much cleaner code abstract as much code from VM to utility classes. 1 state 1 event class MVVM is convoluted. Also, what's wrong with flows? Push all state to VM what's issue with that?

  • @darkskymobs
    @darkskymobs วันที่ผ่านมา +1

    Great video! However, I still feel VMs got legs especially if you want to DI bunch of stuffs.

  • @ashishkharche2345
    @ashishkharche2345 8 วันที่ผ่านมา +9

    i was here when the revolution in android dev began

    • @Zhuinden
      @Zhuinden 8 วันที่ผ่านมา +2

      It started back in 2017 but people just didn't want to choose the simpler way of doing things (even when it was technically and behaviorally more correct). Then again, Android devs always seek out complexity for the sake of complexity, for who knows what reason.

  • @shaharkeisar
    @shaharkeisar 14 ชั่วโมงที่ผ่านมา

    What about Circuit/ Molecule

  • @Naxomiun
    @Naxomiun 7 วันที่ผ่านมา +3

    I dont get the point because you basically did the same as the VM does but with a simplified version that does not cover what you are complaining.
    You will end having the same problems if your screen starts including big features, but instead of having a big VM class youll have 100 extension functions and 100 whens for every state (or a big state for the screen).

  • @kshitijpatil2019
    @kshitijpatil2019 7 วันที่ผ่านมา +1

    What is the harm in providing 2 parameters to a Composable - 1 state and 1 lambda for events? This will make things explicit rather than implicit CompositionLocal.
    Also, I feel the real purpose of VM is structured scoping and state caching. When you navigate to other screen and get back, how are you going to ensure not loading the last things again?

    • @ChristophS
      @ChristophS 5 วันที่ผ่านมา

      Yes, exactly, passing data via navigation is a bad idea due to serialization/desexualization, so we use view models to keep data/state we need in other viewmodels fragments (compose uis)

  • @codingCouncil
    @codingCouncil 6 วันที่ผ่านมา

    Also you are right about JetSnack's convoluted ViewModel . Its a nightmare . However there are several tutorials who show how to do a cleaner ViewModel . One such approach is MVI styled ViewModel .

  • @alexmercerind
    @alexmercerind 8 วันที่ผ่านมา +2

    Ah... official example also passses view model to screen composable... breaking preview...

    • @TheMikkelet
      @TheMikkelet 7 วันที่ผ่านมา +1

      Absolutely this is the worst thing about Compose. Their own frameworks, recommended to use together, do not even work together. It's grave sin to release these tools like this. At least with XML, we bypassed prop-drilling AND we had previews.

  • @coroutinedispatcher
    @coroutinedispatcher 8 วันที่ผ่านมา +1

    Hmmm, and still not just transforming business to ui. It's also lifecycle-aware. It gets out automatically when the activity is destroyed. Plus, you can't just create one just by calling it's constructor. At least in Android. Multiplatform is taking the path of viewmodels as well but I don't know the consequences about that yet. At least I'm OCD-ed by ViewModels. Even when I code in iOS or Flutter, I create a thing called ViewModel 😄

  • @robchr
    @robchr 8 วันที่ผ่านมา

    Before Android ViewModel we had Retained Fragments. It was just an object who's lifecycle survives configuration changes. Of all the Jetpack Libraries, it has the most confusing name.

    • @Zhuinden
      @Zhuinden 8 วันที่ผ่านมา

      Retained fragments were so much easier to use than ViewModel, but the Navigation people couldn't make Navigation work with it so they deprecated it. If we don't use Navigation multi-stack, then retained fragments still work...

  • @jujos132
    @jujos132 8 วันที่ผ่านมา +3

    Wow... It's great to see someone finally addressing the topic of VMs in composables. Using VMs everywhere never seemed ideal. Excellent point! Thanks for sharing about Evas!

    • @Zhuinden
      @Zhuinden 8 วันที่ผ่านมา +1

      There's some irony to the fact that React people just put the callbacks into a "context" which in Android world would be a CompositionLocal and then pass them down like that, instead of every single one as separate callback arguments. However, I think the callbacks belong in the state so that it is scoped to the state in which it is valid to be invoked, UI should know less not more.

  • @arsildo
    @arsildo 8 วันที่ผ่านมา +1

    I liked the video, even though I do not agree with this pattern

    • @s.sellmair
      @s.sellmair  8 วันที่ผ่านมา

      @@arsildo thank you! Really happy for the positivity despite disagreement 😍

  • @yohaneskustiadi9807
    @yohaneskustiadi9807 8 วันที่ผ่านมา

    State and event I thinks it's the best approach right now.
    Because it really makes sense, and you can adapt it to anything.
    Here what have I try.
    Change fragment bottom sheet that using viewmodel into Rxjava, it work perfectly.
    Using compose purely with mutableState and sideEffect without viewmodel.
    So my current approach right now state, event, any reactive library

  • @RahulRavikumar
    @RahulRavikumar 8 วันที่ผ่านมา +2

    ViewModels always felt a bit weird to me too. The problem i have is that don't scale easily as your screen becomes more complex. And the 1 VM per screen adage never made sense to me because you want components to be screen agnostic without the need to write a lot of glue code.
    I, like yourself prefer state hoisting + composition locals when applicable.
    Your video with the examples just reinforced this self discipline I have with the use of ViewModels (avoiding them i.e)

  • @kennynek
    @kennynek 7 วันที่ผ่านมา

    Imo, answer to all those questions is to get experience, then you will know when to create another subcomponent and its VM, when to choose which State approach etc. No one expects Android or any other professional level development to be clear after one year of shool projects.

    • @s.sellmair
      @s.sellmair  7 วันที่ผ่านมา +1

      Fair! I am pretty sure that there are teams that have very good intuition about ViewModels and really like them. I would like to re-emphasize: My point is more that we should be encouraged to use something different if we want to. I tried making it work for me since 2018 and I never really got warm with it. Sure, my profession is not to build Android Apps anymore, I am employed by JetBrains working on Kotlin, IntelliJ, ... I'll try to keep in touch though by having hobby projects.
      Nobody expects you to be great in one year, but honestly: My projects even in 2016, where I did strange freestyle MVC got quite far. I sense that we just accepted that things have to be hard whereas the iOS community often accepts pragmatic solutions.

  • @Alexmez
    @Alexmez 6 วันที่ผ่านมา

    Great point

  • @Zhuinden
    @Zhuinden 8 วันที่ผ่านมา

    The original idea that there is a problem with "State + Callbacks" is valid, state should contain the callbacks so that the operations are scoped to the state in which they are valid to invoke. Also, View should not know what function to invoke in VM to for example "set filtering to XYZ". EVAS is however a bit overengineered and seems not-multi-threading-safe, I do not think this is the right solution. Thrive for simplicity but you're fighting the framework to connect its bits.

    • @s.sellmair
      @s.sellmair  8 วันที่ผ่านมา +4

      > Also, View should not know what function to invoke in VM to for example "set filtering to XYZ"
      In defense of the ViewModels: When a Lambda is passed, I think they do not know directly. They have this one layer (lambda) in between them. Still agree here.
      > EVAS is however a bit overengineered and seems not-multi-threading-safe,
      I would be happy to learn at which points the library is not thread safe, because this would be my highest priority in fixing. It was designed with thread safety in mind, even trying to provide lock free thread safety where possible.
      Also, I hope that this gets across: My point is not that everybody should use what *I like*, but more: "See, I have fun with my own style, that I like, please feel encouraged to find what works for you and build this for *you*"

  • @IsuruKusumal
    @IsuruKusumal 7 วันที่ผ่านมา

    I wonder if molecule can help here

    • @s.sellmair
      @s.sellmair  7 วันที่ผ่านมา +1

      @@IsuruKusumal yes!!

  • @pablovaldes6022
    @pablovaldes6022 8 วันที่ผ่านมา +3

    Finally someone speaks the truth about ViewModels

    • @Zhuinden
      @Zhuinden 8 วันที่ผ่านมา +1

      I remember when Vasiliy Zukanov said the same thing about ViewModels back in 2017

    • @aabhishek4911
      @aabhishek4911 7 วันที่ผ่านมา +1

      @@Zhuinden What vasily mentioned was quite different from sebastian's pain. That post from 2017 does not hold true after introduction of savedStateHandle.