Does anyone know if coroutines have different beha...
# android
j
Does anyone know if coroutines have different behaviour depending on different versions of Android? I got something like
launch(UI){ async(CommonPool, parent=myJob){mySuspendFunction()}.await()}
and when I cancel that job (
myJob.cancel()
) either on my pixel2 running 8.1 or an emulator running the same version it works nicely, but when I run the same code in a nexus 5x running 7.1 it just ignores the cancelation request
p
Did you try this several times? Whether cancellation works may depend on when the cancel request comes in...what does your
mySuspendFunction
look like?
j
Copy code
suspend fun myFunc() = suspendCancellableCoroutine{continuation->
     val disposable =   anObservable().subscribe(onSuccess={continuation.resume(it)})


        continuation.invokeOnCompletion(onCancelling = true) {
            disposable.dispose()
        }
        
    }
something like that
@petersommerhoff
g
Why do you use suspendCancellableCoroutine instead of kotlinx.coroutines integration with RxJava?
p
@julioyg That function will not cancel (and not even suspend) if the future already has a result ready for instance...might be a factor. Otherwise, it should be cancellable.
j
sorry, forgot about that I'm calling
await()
, modifiying the question
@gildor I'm using that coz I didn't know about such integration, looking for it now
but still... would that change anything?
and why does it work on devices 8 and above?
g
ignores the cancelation request
What do you mean? invokeOnCompletion not called?
Maybe you could provide sample project with reproduction of this problem
j
it doesn't execute that code, correct
g
@julioyg are you sure that you don’t lock all the threads of CommonPool, so this just do not allow to execute any other coroutine? try to add
observerOn
before subscription with some background scheduler (Schedulers.io() for example) In general your integration with RxJava is not completed and not particularly correct (for example if observable emit more than one item, your code will crash on contuninuation.resume() call, also you do not handle case of empty observable). Try to check threads and use kotlinx.coroutines rx integration
j
thanks a lot, using another thread for my observable's worked about calling twice to
resume()
i got that controlled, but thank you so much for pointing that out
g
also you don’t need
onCancelling = true
, CancelableContinuation doesn’t not have “cancelling” state and just ignore this argument