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

tapchicoma

10/22/2018, 6:55 AM
Last line returns
true
, but I suppose it should be
false
. Is it a bug, or right behavior?
e

elizarov

10/22/2018, 7:25 AM
That is how it should be. TL;DR It is due to cold/hot mismatch between channels and observable.
👍 1
When you
openSubscription()
you get a channel, which is hot and keeps observer until you close it.
However, when you convert channel to observable with
asObservable
it works just like any other hot->cold converter. The underlying channel is not closed, but you can subscribe to it many time, each subscriber receiving its share of events in fan-out faschion.
Long story short, when you
openSubscription
it becomes your duty to ultimately cancel it.
t

tapchicoma

10/22/2018, 7:28 AM
Should this:
Copy code
downsteamChannel.invokeOnClose {
        upstreamChannel.cancel()
    }
work if I insert this somewhere between
openSubscription()
and
asObservable()
?
t

tapchicoma

10/22/2018, 7:45 AM
yes, this works. Thank you!
3 Views