question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CompletableDeferred<Any>() Cancelation

See original GitHub issue

This code not cancelling. How can i handle this.

fun main(args: Array<String>) = runBlocking<Unit> {
    
    launch {
        getUsers().await()
    }
    
    coroutineContext.cancel()
    
}


fun getUsers(): Deferred<String> {
    
    val call = OkHttpClient.Builder().build().newCall(Request.Builder()
            .url("https://jsonplaceholder.typicode.com/users")
            .build())

    val def = CompletableDeferred<String>()
    
    def.invokeOnCompletion {
        println(def)
    }

    call.enqueue(object : Callback {
        override fun onResponse(call: Call, response: Response) {
            //Not important
        }

        override fun onFailure(call: Call, e: IOException) {
            //Not important
        }

    })

    return def
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fvascocommented, Oct 13, 2018

Dit it cancel the okhtpp call?

Should it do? To cancel the call you have to invoke the cancel method on it.

I strongly suggest you to read the documentation

https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#wrapping-callbacks

Finally consider the code:

suspend fun Request.call(): Response = suspendCancellableCoroutine { continuation ->
    val call = OkHttpClient.Builder().build().newCall(this)
    call.enqueue(object : Callback {
        override fun onResponse(call: Call, response: Response) = continuation.resume(response)
        override fun onFailure(call: Call, e: IOException) = continuation.resumeWithException(e)
    })

    continuation.invokeOnCancellation {
        call.cancel()
    }
}
0reactions
elizarovcommented, Oct 13, 2018

@fvasco Thanks for answering!

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - CoroutineScope - CompletableDeferred cancellation
I want to use this in retrofit2 call adapter, is there any better way to handle this case. fun main(args: Array<String>) = runBlocking<Unit> ......
Read more >
CompletableDeferred - Kotlin
A Deferred that can be completed via public functions complete or cancel. Note that the complete function returns false when this deferred value...
Read more >
CompletableDeferred - javadoc.io
cancel returns true as long the deferred is still cancelling and the corresponding exception is incorporated into the final Deferred.
Read more >
Demystifying CoroutineContext - ProAndroidDev
All the coroutine builder functions like launch and async have the ... sure that the CompletableDeferred is properly cancelled would be to ...
Read more >
Rx to Coroutines Concepts, Part 3: Deferred & Channels
Any screen should be able to call sendMessage and have the result show ... CompletableDeferred<ChatSession>() override suspend fun session() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found