CompletableDeferred<Any>() Cancelation
See original GitHub issueThis 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
@fvasco Thanks for answering!