Stop Obsessing Over Dispatchers: Coroutine Architecture's Real Pillars Are Main-safe, Non-blocking, and Structured Concurrency
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.
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.
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.