Scope builders—coroutineScope, supervisorScope, withContext—structure concurrent work with parent-child cancellation. Compare with errgroup in Go: failures in one child can cancel siblings when using standard scopes.
Builders overview
coroutineScope— waits for all children; fails fast if one child throwssupervisorScope— child failures do not cancel siblingswithContext(Dispatchers.IO)— switches dispatcher for blocking I/O
runBlocking bridge
Tests often use runBlocking { coroutineScope { ... } } to assert concurrent behavior. Production Android uses viewModelScope or lifecycleScope instead of manual scopes.
Important interview questions and answers
- Q: supervisorScope use case?
A: Independent tasks where one failure should not tear down others—e.g. loading multiple UI sections. - Q: withContext purpose?
A: Switch coroutine context (dispatcher, name) for a block—commonly move blocking work off Main.
Self-check
- Which scope isolates child failures?
- Why use withContext for database calls on Android?
Pitfall: Never use GlobalScope for app logic—prefer structured scopes tied to lifecycle or request.
Interview prep
- coroutineScope?
Structured concurrency—child failure cancels siblings.
- withContext?
Switches dispatcher for IO/Main/Default work.