kotlinx.coroutines
kotlinx.coroutines copied to clipboard
about Flow<Flow<T>>.flattenMerge and stateflow.
I see the 1.8.0RC add the method flattenMerge for flatten flows,so I want know that if there has some stateFlow in one StateFlow: like this
val stateFlowParent:StateFlow<StateFlow<Int>>
...
stateFlowParent
.flattenMerge()
.onEach {
//Is there a repeat output here?
}.launchIn(someScope)
More specifically, due to the long life of Stateflow, when a Stateflow is returned in flatmap, the Stateflow will not be canceled (I do not know how to cancel, unless I add a job variable to control the collection and cancellation of Stateflow). So the allowable parameter of the newly added flattenMerge is Stateflow? Thanks, it depends if I want to upgrade my repository version.
@dkhalanskyjb hi my friend,maybe my intention and #4008 are not the same issue.
The scenario I'm referring to occurs when there is nested hot flow.In this case, I would subscribe to the second layer of flow again when there is a change in the first layer of flow. However, since the lifecycle of the hot flow does not end automatically, if the second layer of flow is subscribed multiple times, it will result in an exponential increase in the number of subscriptions. If there is a need to expand nested hot flows, we currently provide methods for this purpose flattenMerge
. However, based on observation, these methods only serve cold flows, and there is an issue of subscription explosion with hot flows.
A successful expansion should involve canceling previous subscriptions when there is a change in the first layer of flow and the need to expand the second layer of flow. This way, it is possible to safely collect the latest flow.
Forgive my ignorance, but it seems that flatMapLatest can solve this situation. It automatically cancels the flow returned by the flatMapLatest code block, regardless of whether it's a hot flow or a cold flow. Is that correct? I would like a definite answer. Thank you very much!
For a hot flow, cancelling only cancels the subscription, not the upstream flow itself. For usage questions, please use Stack Overflow or Kotlin Slack.
For a hot flow, cancelling only cancels the subscription, not the upstream flow itself. For usage questions, please use Stack Overflow or Kotlin Slack.
Understood. My intention was indeed to cancel the subscription to the hot flow. Thank you.