What I want to achieve?
I have a task to download images but as the screen scrolls it will cancel previous downloads and start downloading new ones. I want that when it cancel the coroutine downloading previous image it stop instantly and free up the bandwidth so new images download faster.
What I have tried?
I have tried multiple ways to stop the coroutine but it keeps going until it finishes the downloading even after cancelling the coroutine. When I cancel the coroutine it makes a variable isActive to false and stop calling further suspended function. But the problem is if its running a loop for 1000000 times or downloading an image from network this task will not canceled unless completed. Like loop will complete it's 1000000 iterations then the coroutine will be cancelled.
I have tried these but no success:
job.cancel()
scope.cancel()
I have tried so many ways for achieving this but got no solution. I can't use any library right now in my project.
This use case not achieved by Threads, Executor Service, Coroutines. Because all behave the same.
More questions same like this :
How do I cancel a kotlin coroutine for a blocking download operation
AsyncTask is not cancelling the long running operation in android