https://kotlinlang.org logo
#compose
Title
# compose
p

prudhvi reddy

03/19/2024, 6:17 PM
Hey 👋 Folks I'm seeing both
collectAsState
and
collectAsStateWithLifecycle
is taking in a param
context
Should i be passing
viewLifecycleOwner.lifecycleScope.coroutineContext,
Copy code
val state by viewModel.orderDetailsLiveData.asFlow().collectAsState(
    initial = null,
    context = viewLifecycleOwner.lifecycleScope.coroutineContext,
)
To collect the state properly when i'm using fragments + compose ? When should someone be using
context
param ?
i

Ian Lake

03/19/2024, 6:37 PM
It has a default because you usually don't need to change it. It is rare that it matters what context you collect on
p

prudhvi reddy

03/19/2024, 6:51 PM
🙇 Thanks for the reply Can you think of a case where i might want to collect it in a different context ? I can see this code after going down a bit. Was thinking when will someone be passing a different context
m

Michael Krussel

03/19/2024, 8:43 PM
Since you started with a LiveData, would it make more since to use the LiveData state binding instead of converting to flow and then collecting the flow. I guess it might be an extra library, but then you might be able to remove the library that converts from LiveData to Flows. https://developer.android.com/jetpack/compose/state#use-other-types-of-state-in-jetpack-compose
p

prudhvi reddy

03/20/2024, 7:41 AM
Yeah. that can be changed.
2 Views