StateFlow vs. Compose State in a ViewModel

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

This would be a great idea for video. I recently switched to compose state because it seems more natural when working with compose. However, I understand your point about the advantages of StateFlow compared to compose state. There were some extra steps when using stateflows, especially when collecting them in the composables because collectAsState is not safe enough (the lifecycle isn't taken into account). Can you please make a longer comparison video about these two. Btw, your videos are great and helped me a lot in my career.

СтефанТодоровски-ья
Автор

Keeping ViewModel Compose-free makes a lot of sense.

ubersticks
Автор

Sometimes compose state in viewmodels throwing snapshot not initialized which is kinda sucks

meidy
Автор

Process death can be handled with the rememberSaveable { } block

alonshlider
Автор

Hey Philipp - it would be great to see a new tutorial to demonstrate how you keep the ViewModel free of Compose. It is hard not to use the Compose function "mutableStateOf()" in moderately complicated VMs. You can take that opportunity to show all your up-to-date recommendations (like use of remember{ derivedState() }

ubersticks
Автор

What is the correct way to bind to StateFlow in your composables then?

scotty
Автор

Do StateFlows exposed from ViewModels introduce more overhead compared to Compose State? If they do, there is a good reason to not use StateFlows as a source of truth for Compose, since you probably wouldn't want your user's text inputs to lag

inertia_dagger
Автор

Couldn't agree more. StateFlows are more mature.

I'm not sure how testable are State(s). Haven't tried that yet.

thelumiereguy
Автор

Bro if you feel stress ..
Just create an Interface class..
which takes in a lambda expression or a function pointer to store the long process of callback function..
And then inside that interface you have 10 methods to override that does not contain anything..

orangasli
Автор

I believe in using the right tool for the right situation. So far states were the natural tool to use in my use cases. Will keep giving flows a thought as I develop new things. I'm curious about the use case when it'll actually click and make perfect sense to use.

st
Автор

I am sorry, I am new to this thing so I need to clarify one thing.
Is this what you mean?

private val _state = mutableStateOf()
val state: State = _state

change to

private val _state = MutableStateFlow()
val state:StateFlow = _state

Jerry-ofzj
Автор

You should never use composestates outside of composables. It can lead to weird, hard to catch illegalstate exceptions.

dislike__button
Автор

Bro does interface is only used for callback events?
For example if I want to write my own setOnClickListener interface on my own from scratch..
So I need to handle events that are related to clicking a View like for example if the use click on the appropriate region of the screen to trigger that interface..
So there must be code somewhere..
In C++ we use fnction pointer as parameter to a callback function ..
In kotlin we use lambda expression..
And so how do you make a callback function as an Interface..
It's like saying I want to use this
But according to my need..
Also I cannot change how the interface work..
Really mind boggling

orangasli
Автор

Hi Philipp, how many experiance years do you have in android development? + Thank u so much for your amazing content

hossamqandel
Автор

Hey man I need to learn how to share viewmodels between two or more composable functions. I saw how to do it in one of your videos but I can't remember which video. So could you be able to give me that video link

ajay-riex