I have a source flow that I want to map,filter in ...
# coroutines
a
I have a source flow that I want to map,filter in different ways. Would I need to create multiple flows and launch&collect them from separate coroutines, or is that a bad thing and there is a better way to chain it within a single coroutine? The coroutine is dispatched to Main, so I'm concerned on launching many separate coroutines on it (though not sure the cost of doing so)
Let's say I have a flow of a sealed class Error types. I want to filter to each type and do further transformations for each of them.
s
You have to consider that a flow may not produce a value until the consumer asks for it. You can't do things like peek at the current value and decide whether to consume it or not. Emitting the value and collecting the value are the same thing. So you can't have multiple consumers, each choosing which should consume the value. For that, you would need to use something like a
SharedFlow
.
Coroutines are lightweight, don't worry about creating lots of them 👍
👍 1
Use
shareIn
to make your flow into a shared flow, and then launch separate collectors for each type of value you want to handle
(Or, if your source flow is something you can make multiple copies of, just do that and don't bother with
shareIn
)
a
Yes, I am using sharedflow in this case
I see, thank you