efemoney
05/19/2019, 7:39 PMFlow
is cancelled/disposed. Something like ObservableEmitter.setCancellable { ... }
in the rx world.
I want to use Flow
to model an add/remove listener so that a listener is attached when the flow is “subscribed” to and detached when the subscription is “disposed”. Forgive the rx lingo, pretty new to Flow
so I will also appreciate reading material that can tie or at least differenciate between my RxJava knowlege and Flow
octylFractal
05/19/2019, 7:40 PMonErrorCollect
or onErrorReturn
?cancel
operationefemoney
05/19/2019, 7:46 PMFlow
tseisel
05/19/2019, 8:03 PMflowViaChannel
is what you need : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow-via-channel.html
You can't directly cancel a Flow
, but you can cancel the coroutine it is running in.
This will cause the underlying channel to be closed, triggering its invokeOnClose
block and thus removing your listener.efemoney
05/19/2019, 8:32 PMelizarov
05/20/2019, 5:24 AMflow {
try {
while (isActive) {
/* emit elements */
}
} finally {
/* called when it is cancelled */
}
}