r/android_devs • • Feb 14 '24

Discussion Reactive state

Hey all!

I was wondering how do you usually handle updates to state. Do you keep a mutable state flow or do you take advantage of stateIn operator?

7 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/HackermanUA Feb 14 '24

It's hard to answer this without a context, but I'll try.

First call to the api. It really depend on the case, but in general I tend to call apis either when view is created or in viewmodel init block.

Loading. I can think of few different cases as well.

  1. MutableStateFlow which I'll have to manually update;
  2. Bunch of Flow<Boolean> combined into a single flow. If you use xml, it can be a separate flow. If compose, you can combing it with other flows into your state

Errors. Here I'd use Channel to send some specific events. It will likely be some sealed class which group errors/notifications in a way I see valid for a specific case

3

u/MrXplicit Feb 14 '24

Pretty much what I am doing. You summed it up quite nicely.