android - Job.cancel() does not cancel coroutine - Stack Overflow

I have a small test like this. suppose the getAll() function is getting data from api. I use the delay(

I have a small test like this. suppose the getAll() function is getting data from api. I use the delay(3000) function to simulate the delay.

class Repository {
    var job: Job? = null
    fun getAll() {
        job = CoroutineScope(Dispatchers.IO).launch {
            try {
                delay(3000)
                println("getAll oke")
            } catch (e: Exception) {
                println("CancellationException")
            }
        }
    }

    fun cancel() {
        job?.cancel()
        println("cancel")
    }
}

fun main() {
    runBlocking {
        val api = Repository()
        launch {
            api.getAll()
        }
        delay(1000)
        api.cancel()
        delay(5000)
        println("END")
    }
}

In the above paragraph it will print out like this:

cancel
CancellationException
END

If I replace delay(3000) with Thread.sleep(3000).

cancel
getAll oke
END

Problem: If I replace delay(3000) with Thread.sleep(3000). It not work. I can explain. The reason is because delay() knows whether the coroutine has been called cancel() or not. If it has been canceled, it will automatically cancel that coroutine.

But Thread.sleep() does not. So even though cancel() has been called. the statements in the coroutine will still run.

My question is. If the getAll() function makes an Api call using retrofit, room... will it be canceled when cancel is called?

I have a small test like this. suppose the getAll() function is getting data from api. I use the delay(3000) function to simulate the delay.

class Repository {
    var job: Job? = null
    fun getAll() {
        job = CoroutineScope(Dispatchers.IO).launch {
            try {
                delay(3000)
                println("getAll oke")
            } catch (e: Exception) {
                println("CancellationException")
            }
        }
    }

    fun cancel() {
        job?.cancel()
        println("cancel")
    }
}

fun main() {
    runBlocking {
        val api = Repository()
        launch {
            api.getAll()
        }
        delay(1000)
        api.cancel()
        delay(5000)
        println("END")
    }
}

In the above paragraph it will print out like this:

cancel
CancellationException
END

If I replace delay(3000) with Thread.sleep(3000).

cancel
getAll oke
END

Problem: If I replace delay(3000) with Thread.sleep(3000). It not work. I can explain. The reason is because delay() knows whether the coroutine has been called cancel() or not. If it has been canceled, it will automatically cancel that coroutine.

But Thread.sleep() does not. So even though cancel() has been called. the statements in the coroutine will still run.

My question is. If the getAll() function makes an Api call using retrofit, room... will it be canceled when cancel is called?

Share Improve this question edited Nov 19, 2024 at 10:29 tyg 16.9k4 gold badges40 silver badges49 bronze badges asked Nov 19, 2024 at 6:32 Nguyễn Mạnh CườngNguyễn Mạnh Cường 6182 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

Cancellations in coroutines are cooperative. This means the coroutine has to cooperate to be properly cancelled, it has to check the cancellation status in regular intervals and throw if needed. Thread.sleep is not at all aware of coroutines, so it simply ignores cancellations. Same if we do a blocking I/O or CPU-heavy computation (without additional checks) - they can't be cancelled.

There is no generic answer to the question if 3rd party libraries properly handle cancellations. Most of good and popular libraries designed for coroutines should support cancellations - this includes Retrofit and Room. But you need to check the documentation of a given library to make sure.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583599a4634399.html

相关推荐

  • android - Job.cancel() does not cancel coroutine - Stack Overflow

    I have a small test like this. suppose the getAll() function is getting data from api. I use the delay(

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信