into a different one?
This is what I've done, not sure if it's the best or even working...
Copy code
val newStateFlow = originalStateFlow
.map { it.doTransform() }
.stateIn(scope = CoroutineScope(Job()), started = SharingStarted.Eagerly, initialValue = false)
j
Joffrey
02/25/2022, 8:35 AM
You should never create a CoroutineScope on the fly like this without storing it into a variable. You need to control the cancellation of the coroutines that it manages by cancelling this scope at an appropriate time. Converting any flow into a state flow implies that a coroutine collects the source flow to update the state, and that work needs to stop when not needed anymore (state flows are hot)
Joffrey
02/25/2022, 8:36 AM
Apart from that, I guess it's the correct way
d
David W
02/25/2022, 12:30 PM
Thank you. It's used in a singleton and is intended to exist for the lifetime of the application.