https://kotlinlang.org logo
#coroutines
Title
# coroutines
b

benny.huo

01/18/2020, 2:13 PM
PublisherAsFlow ignores the CoroutineContext, so that it make no sense to call flowOn on it. Is it a bug? I have opened an issue for this: https://github.com/Kotlin/kotlinx.coroutines/issues/1765
b

bdawg.io

01/18/2020, 5:02 PM
That's because flows inherit the scope for its operators based on the context that the
collect
is called from. A publisher should already have it's schedulers registered before it's adapted to a Flow. If you don't have any schedulers on your Publisher, your
flowOn
operator will set the context that the
collect
is executed against
b

benny.huo

01/18/2020, 10:36 PM
The
flowOn
operator will not set the context, that's the problem.
b

bdawg.io

01/19/2020, 3:27 PM
I see now. In the implementation, it looks like they are explicitly abstracting the collection context away from the reactive stuff through a channel. So the logic that opens the scription happens on your IO thread, but it doesn't influence the publisher itself. The only thing the context is used for is if that reactive streams implementation has its own version of a context, then it supplies that context so it's accessible through the coroutine context. See
ReactorContext
https://github.com/Kotlin/kotlinx.coroutines/blob/1.3.3/reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt I could see why they wouldn't manipulate the reactive streams' scheduling. We'll see what they say in that issue I guess
2 Views