-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add start parameter to launchIn extension for Flow #1326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you give a slightly more context in your example, please (some more code around it). Where is your code that is trying to collect the flow? And what kind of flow you are collecting? I'm trying to grasp why it is important to be |
This is my use case: import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
suspend fun main() {
coroutineScope {
val eventBus = BroadcastChannel<Int>(5)
val subscription = eventBus.asFlow()
launch(/* start = CoroutineStart.UNDISPATCHED */) {
subscription.collect { println(it) }
}
repeat(10) { eventBus.send(it) }
eventBus.close()
}
} I used the same trick, an active subscription should not lost messages. |
@elizarov, is the following example meaningful to you? someFlow.onEach { // Start of collection of someFlow enables UI component.
ui.updateUi(it) // If value is already available, we might benefit from
// updating UI ASAP, not waiting for an extra dispatch.
}.launchIn(this) // We would prefer it to be undispatched to avoid 2 layout passes. |
Would simply using |
It wouldn't exactly, as it would avoid dispatch for every coming value as opposed to the first one until the first suspension point. Using Now that I'm thinking about it though, I wonder if this is plausible as even dispatched, collecting a flow leads to a suspending function to be called, which always passes through a dispatch through the ContinuationInterceptor making synchronous collection of first value impossible, right? |
Both Indeed, |
#1340 can fix my use case |
@qwwdfsad Is the behaviour for A:
equivalent to B:
I would have thought so, given the intention of I have a bug in a test (only on the test environment) where A does not happen synchronously but B does. There are other complications involved ( |
Usage would look like so:
My current use-case is to have the UI be set up synchronously by the flow collection start, avoiding an extra unneeded layout pass in an Android app in case collecting the flow has such side-effects.
The text was updated successfully, but these errors were encountered: