Sobes.tech
Junior — Middle

What is the main functionality and purpose of SupervisorScope in the system?

sobes.tech AI

Answer from AI

SupervisorScope is a special CoroutineScope in Kotlin Coroutines used to manage a group of coroutines.

Main functionality:

  • Allows launching multiple child coroutines, where failure of one does not cancel the others.
  • Unlike a regular CoroutineScope, where an error in a child coroutine cancels all, SupervisorScope isolates errors.

Purpose:

  • Used when you need to run several independent tasks in parallel and handle their errors separately.

Example:

val supervisor = CoroutineScope(SupervisorJob() + Dispatchers.Default)
supervisor.launch {
    // coroutine 1
}
supervisor.launch {
    // coroutine 2
    throw Exception("Error") // will not cancel coroutine 1
}