ritesh
04/12/2022, 6:49 PMSafe Flow collection in Jetpack Compose
@Composable
fun LocationScreen(locationFlow: Flow<Flow>) {
val lifecycleOwner = LocalLifecycleOwner.current
val locationFlowLifecycleAware = remember(locationFlow, lifecycleOwner) {
locationFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
}
val location by locationFlowLifecycleAware.collectAsState()
// Current location, do something with it
}
Do we need it, while collecting states? Isn't Composables
have their own life-cycle? and we need not to worry about how the lifecycle of host it's contained in, while collecting flows?Adam Powell
04/12/2022, 6:58 PMritesh
04/12/2022, 7:23 PMstate-flows
if my app moves to background, new emit
from api or so before moving to background, will just update the view, compose or re-compose while in background.ritesh
04/12/2022, 7:25 PMsate-flow
collection, host life-cycle aware, how composable gets notified when there is change in lifecycle from STARTED
to PAUSED
or so, considering the mentioned scenario where app goes in background and then comes back to foregroud.ritesh
04/12/2022, 7:28 PMAdam Powell
04/12/2022, 7:34 PMAdam Powell
04/12/2022, 7:34 PMAdam Powell
04/12/2022, 7:35 PMritesh
04/12/2022, 7:40 PMStateFlow
which is a hot-stream, and whose emission is usually based on some click event or say one-shot api call, in most of the apps or screens.
In above scenario it's probably an over-kill IMO, and doesn't makes much to make this collection host life-cycle aware.ritesh
04/12/2022, 7:49 PMstate-flow
collection - host activity life-cycle 😄 aware unless required. And in most of the use-cases we probably don't need it i guess!ritesh
04/12/2022, 9:50 PMritesh
04/13/2022, 12:31 AMIf you're using the default Recomposer that gets configured when you use ComposeView or ComponentActivity.setContent then recompositions will stop when the lifecycle does and start again when the lifecycle does.As this is the default behavior, one should not worry about re-composition when app is in background, re-comp stops when it's host life-cycle does. Considering above, it's more of a concern if you are collecting cold-stream (few examples like location update or open socket connection etc...) or any stream which is effectively updating some state even if the app is in background and to avoid that collecting flows using
.flowOnLifecycle
makes sense.
But, if you have something like StateFlow (a hot stream), whose emission is not very frequent but limited to one-shot api-calls etc.. .flowOnLifecycle
could be totally avoided, as re-comp won't take place when app is in back-ground as it adheres to the host lifecycle and updating state is fine.Adam Powell
04/13/2022, 1:02 AMAdam Powell
04/13/2022, 1:04 AM.stateIn(..., SharingStarted.WhileSubscribed...)
then you should also ask why it's not a mutableStateOf
instead 🙂ritesh
04/13/2022, 1:27 AMmutableStateOf
instead of StateFlow
- i know that somewhere down the line when we do collectAsState
it converts to State<>
I have been using StateFlow
for every screen (even if i don't need to use transformation or other operators) and have found it pretty handy when it comes to testing using libs like turbine
etc..
Say, i don't want to use operators on flows, i should be prefer mutableStateOf
? Probably wrap all my mutableStateOf
inside one class and then expose it to composable?ritesh
04/13/2022, 1:38 AMAdam Powell
04/13/2022, 1:40 AMAdam Powell
04/13/2022, 1:41 AMritesh
04/13/2022, 1:44 AMwe might publish some additional guides down the line since it comes up fairly frequentlythat would be great! This thread has detailed explanation! Thanks Adam!