elizarov
09/11/2017, 7:31 AMUnconfined
context, which leaves the thread “unspecified”.jw
09/11/2017, 1:12 PMcoroutineContext
) and the target context so I figured that I could replicate observeOn by having the current context loop and then send values across a channel onto a loop in the target context.elizarov
09/11/2017, 1:50 PMUnconfined
context gives you similar, but not really the same behavior as Rx. Consider the following pipeline:
produce(Context1) { ... }.map(Unconfined) { ... }.consume(Context2) { ... }
So, you have a producer running in one thread and a consumer in another thread, with unconfined transformer in between. What thread the transformer runs in? The answer — it depends. With standard channel implementations from kotlinx.coroutines
the transformer sometimes runs in one thread, sometimes in the other. If the producer is faster than the consumer, then the producer is resuming suspended transformer coroutine, thus the transformer runs in the first thread. However, if the consumer is faster than the producer, then the consumer is resuming suspended transformer coroutine and thus transformer runs in the second thread. In order to imitate Rx behavior you either have to explicitly inherit producer’s context or exercise special care about the kinds of channels you are using in between the coroutines.