Here is the piece of code I have running on Dispatcher.IO thread pool
suspend fun download(rquest: YoutubleDLRequest){
withContext(Dispatchers.IO){
YoutubeDL.init().execute(request) // downloads media in background
}
}
Now, when user clicks a button, the download starts - it either fails due to network error, or completes. My question is how do I cancel this download operation? I have another button to allow the user to cancel the download operation. If I wrap withContext in a launch and keep a reference to the job and then try to cancel the job on the button click, it will not work. I know, I need another suspension point, I tried calling another suspend function with yield inside a while loop. But, the while loop does not let the download code execute at all. I don't understand, how cancel would be implemented in this scenario. Any help would be appreciated.