Is there anyway to know if a Job has been cancelle...
# coroutines
a
Is there anyway to know if a Job has been cancelled other than checking
isActive
or having a CancellationException thrown?
o
what other way are you looking for?
a
A callback? Maybe I need to think more in coroutines. Bascially I want to subscribe to an Observable and keep the subscription until the Job is cancelled - after which is dispose.
z
you can call
join()
, which will suspend until the job completes or fails
☝️ 1
o
there is
invokeOnCompletion
a
right now I have a:
Copy code
try {
delay(MAX_LONG)
} finally {
dispose()
}
z
but yea for that use case you probably want
invokeOnCompletion
o
but depending on where the Observable comes from, there is well-tested adaptor libraries
a
I didn’t find anything in the kotlinx-reactive one
z
Where is the Job coming from?
It’s a Supervisor Job associated with a View
z
just turn it into a Flow and collect it from a coroutine that is a child of that job
openSubscription
should work if you use
consumeEach
, but it is deprecated
a
I guess that is it
z
collect { }
will cancel correctly if its coroutine is cancelled
and that will dispose the rx subscription
a
Thanks - Flow just didn’t feel like the right use-case here. The stream is not really lazy, but this will work.
z
and I believe
asFlow()
is stable as of coroutines 1.3.2
a
Oh I’m a couple versions behind 🙃
woops
z
Lazy or not, Flow is basically the official coroutines-friendly analogue to Rx now, so it’s probably the right use case.
a
Yeah - looks like it. 👍
I think you need to convert the Observable to Flowable first correct;/
I think you need to convert the Observable to a Flowable first correct?
z
Oh yea, that’s true