When to use coroutineScope vs supervisorScope?

前端 未结 2 1921
臣服心动
臣服心动 2021-02-13 17:25

Can someone explain what exactly is the difference between these two?

When do you use one over the other?

Thanks in advance.

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 18:02

    I think Roman Elizarov explain it quite in details, but to make it short:

    You have this kind of hierarchy:

    • Parent Coroutine
      • Child coroutine 1
      • Child coroutine 2
      • ...
      • Child coroutine N

    Assume that "Coroutine i" fails. What do you want to happen with its parent?

    If you want for its parent to also fail, use coroutineScope. That's what structured concurrency is all about.

    But if you don't want it to fail, for example child was some kind of background task which can be started again, then use supervisorScope.

提交回复
热议问题