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

julioyg

04/23/2018, 1:47 PM
👋 I need a bit help, I'm trying go from an async complex op in rx to a simpler implementation using coroutines, so first step is to get my observable returning a value, to do so I'm using
suspendCoroutine
so:
Copy code
suspend fun myFunc = suspendCancellableCoroutine{continuation ->
  myObservable.subscribe(onsuccess={continuation.resume(it)}, onError={continuation.resume(error(it)})
   continuation.invokeOnCompletion(onCancelling = true) {disposable.dispose()}}
Thing is that when I cancel the coroutine that calls the function, it doesn't dispose the observable, is there anyway to make the function to cancel immediately? as the doc says even if the coroutine is cancelled it doesn't mean that the code will be cancelled but instead we should check regularly inside that code whether is't cancelled or not, but, is there any alternative?
v

Vsevolod Tolstopyatov [JB]

04/23/2018, 3:31 PM
Hi, for every coming subscriber you have to register its cancellation. Essentially something like
onSubscribe = { d-> continuation.disposeOnCompletion(d)}
But it seems like you are trying to implement whole rx integration by yourself. I encourage you to take a look at https://github.com/Kotlin/kotlinx.coroutines/tree/master/reactive and use it instead. Feel free to create issues if something is missing
6 Views