跪拜 Guibai
← All articles
Android

Stop Obsessing Over Dispatchers: Coroutine Architecture's Real Pillars Are Main-safe, Non-blocking, and Structured Concurrency

By 潜龙勿用之化骨龙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Misplaced thread switching and unscoped coroutines cause blocked main threads, leaked tasks, and unpredictable cancellation. Making Main-safe and structured concurrency the contract prevents these failures at the API boundary, so apps stay responsive and cancellations behave predictably.

Summary

Most Android coroutine discussions obsess over choosing Dispatchers.IO or Dispatchers.Default and when to wrap calls in withContext. That focus misses the point. The real architectural pillars are three: a suspend function must be Main-safe by contract, meaning the caller never thinks about which thread runs the blocking work; a truly non-blocking call suspends and releases the thread instead of occupying it with a sleep; and every concurrent task must belong to a structured scope so it can be awaited, cancelled, and managed. Dispatchers are implementation details. A mature data layer uses non-blocking APIs such as Retrofit, Room, OkHttp, and Flow, isolates any legacy blocking calls inside its own boundary, and exposes only Main-safe suspend and Flow interfaces. Domain and presentation layers then focus on business logic and UI state without ever deciding threading. Abandoning random CoroutineScope creations and ad-hoc async patterns in favor of coroutineScope-structured parent-child relationships gives operations clear ownership and propagation. The article argues that the sign of coroutine maturity is upper layers no longer caring about threads at all.

Takeaways
— Main-safe means the API guarantees it runs safely from any context, usually by isolating blocking I/O itself; callers should never need withContext.
— A suspend modifier alone does not make a function non-blocking; if the underlying call blocks a thread, the thread is still occupied.
— Prefer non-blocking suspend APIs (Retrofit, Room, OkHttp async, Flow) over adding withContext(Dispatchers.IO) around them.
— Data layer owns thread isolation for any legacy blocking calls; upper layers receive Main-safe suspend/Flow APIs.
— Structured concurrency requires child coroutines to be launched within a parent scope (e.g., coroutineScope), so the parent awaits their completion and cancellation propagates.
— Avoid creating independent CoroutineScopes in the data layer or ad-hoc scopes in business logic; tasks should have clear lifecycle ownership.
— Dispatchers.IO and Dispatchers.Default have distinct uses (blocking I/O vs CPU-bound), but selection is an implementation detail, not the core of architecture.
Conclusions

The article deliberately downplays dispatcher selection: in practice, many calling sites use withContext(Dispatchers.IO) defensively, not because the underlying API blocks. That defensive habit hides the real async capability and shifts responsibility to the caller.

The key test is whether a suspend function actually releases the thread. Thread.sleep under a dispatcher still occupies that thread; delay suspends and frees it. This distinction is often overlooked.

Structured concurrency is framed as the answer to leaks and unmanageable tasks: creating a CoroutineScope inside a repository severs the lifecycle link, making cancellation and waiting impossible. Using coroutineScope or supervisorScope ties tasks to the caller's scope.

The article argues that a good data layer makes the entire app more robust by exposing only suspend and Flow APIs, so even if a blocking library is swapped later, the callers are unaffected.

Concepts & terms
Main-safe
An API that can be called from the main thread without blocking it. Usually achieved by internally switching to a background dispatcher or using non-blocking suspend calls.
Non-blocking suspension
A suspend function that releases the thread while waiting, e.g., delay() versus Thread.sleep(). The coroutine is paused but the thread is free.
Structured concurrency
A principle that concurrent tasks are bound to a parent scope, so the parent must wait for children and cancellation propagates. Implemented via coroutineScope or supervisorScope in Kotlin.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗