Async is not cancelled
See original GitHub issueI have a function which load the image by click on button. If button is clicked twice (second click before async completion) it’s needed to cancel old async and start new. Code example:
private var deferred: Deferred<Bitmap>? = null
override fun onCreate(inState: Bundle?) {
super.onCreate(inState)
findViewById<View>(R.id.btn_action).setOnClickListener {
if (deferred != null && deferred?.isActive == true) {
Log.d("debug", "cancel")
deferred?.cancel()
}
loadImage()
}
}
private fun loadImage() = launch(UI) {
Log.d("debug", "start")
deferred = async {
val conn = URL("http://elitefon.ru/pic/201503/2560x1600/elitefon.ru-38503.jpg").openConnection() as HttpURLConnection
val bitmap = BitmapFactory.decodeStream(conn.inputStream)
Log.d("debug", "end of async")
return@async bitmap
}
deferred?.await()
Log.d("debug", "loaded")
}
In this case log will be something like this:
start
cancel
start
end of async
end of async
loaded
As we can see, in spite of cancel, “end of async” log is called twice. Also, try/catch logging shown that CancelationException is throws in the launch(UI) block (not the async block). Can I make it so that if I cancel async, the exception was thrown in the async block?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Swift async let cancellation doesn't work - Stack Overflow
Your "sleep" is not "cancellable", meaning it does not check any "cancellation point", thus it will run till completion. Please read Task ...
Read more >Perspectives on Async Cancellation - Eric Holk
Lately there has been a lot of discussion about async cancellation in Rust. One thing I've noticed in these discussions is that cancellation...
Read more >Async Cancellation I — 2021-11-10 - Yoshua Wuyts
This means the first cancellation point of any future is immediately after instantiation before the async functions's body has run.
Read more >Async let cancellation - Bug? (confused) - Using Swift
The child task is marked as cancelled, but it is running in the "background" and the parent task is not notified of its...
Read more >Cancel async tasks after a period of time (C#) - Microsoft Learn
You can cancel an asynchronous operation after a period of time by using the CancellationTokenSource.CancelAfter method if you don't want to ...
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
@fvasco, thanks for your answer. I developed the similar solution:
What do you think about this?
@fvasco, thank you so much for your answer. I hope that this functionality will be able in kotlin-coroutine in a future as are described in #57.