what is the best way to expose state from `ViewMod...
# compose
m
what is the best way to expose state from
ViewModel
? am using compose’s
MutableState
but I see a lot of examples with
Flow
but I don’t see any need for using
Flow
to expose state in pure compose app. is
Flow
used in app in transition from
View
to
Compose
to bridge the gap and use some of the existing code or
Flow
is for some reason more favourable than
MutableState
?
c
I've been using snapshot state (mutableState*Of) and from what I've read from Adam Powell, it makes sense because even if you use Flow or LiveData, you still need to call collectAsState somewhere which turns it into snapshot state anyway. To me, it seems like you should only use a flow/livedata if it's out of your control/somewhere upstream is using those observable patterns.
🦜 1
2
m
exactly
c
My comment above comes with the caveat that I actually understood Adam correctly. 🙃
a
Close enough 🙂 Flows are useful if you have cold data sources but if you're using MutableStateFlow anywhere you're probably better off with mutableStateOf instead
👍 1
👌 1
c
Still need to read up on hot vs cold flows 😅
m
The only benefit you get using
LiveData
or
Flow.flowWithLifecycle
is that you’re going to avoid recompositions when the app is in the background. If you don’t care about that and you don’t need the power of Flow operators (that shouldn’t be needed for uiState), then
mutableStateOf
is perfect!
👍 1
m
what is
flowWithLifecycle
actually doing in compose world?
a
Recompositions in the background aren't generally worth worrying about. Compose currently halts all recomposition in the background anyway (though future versions will probably just throttle it instead). What you have to worry about is the ongoing work performed by the producers of cold data sources
👍 3
If you subscribe to a flow that listens to realtime GPS location updates, for example, that has a non-trivial power cost on a mobile device, not to mention user privacy expectations
👍 1
m
I see. thanks all