Concurrency
Kotlin coroutines + Flow, Swift async/await + actors, and keeping work off the main thread. Cancellation on lifecycle end prevents the leaks interviewers love to ask about.
The mental model
Picture a single cashier at the front of a shop — the main thread — who is the only person allowed to update the display board that customers watch. Everything the customer sees goes through this one cashier. If the cashier walks to the stockroom to count inventory (a network call, a database read), the display freezes and the queue stops moving. So you hire runners (background threads) to do the counting and the fetching, and they hand the cashier only the finished total to post on the board. The cashier's job is to stay at the counter and keep the board moving; anything slow is delegated.
That is the whole of mobile concurrency in one image, and it exists because of the constraint from the runtime lesson: the main thread renders every frame in ~16 ms and is the *only* thread allowed to touch the UI. So concurrency on mobile is not about raw parallel throughput the way it is on a server — it is about keeping slow work off the one thread that draws, then delivering results back to it safely.
Modern platforms give you *structured concurrency* to do this without the classic footguns. The old way — spawn a thread, hope it finishes, remember to cancel it, marshal the result back by hand — leaked memory and updated dead views constantly. Kotlin coroutines and Swift async/await replace that with a model where concurrent work lives inside a *scope* tied to a lifecycle: when the scope ends (the screen closes, the ViewModel clears), every child job is automatically cancelled. The mental upgrade is from "start work and manage its death manually" to "launch work into a scope that owns its lifetime for you".
The one idea interviewers keep probing: **cancellation is not an afterthought, it is the point.** A coroutine in viewModelScope that is cancelled when the screen closes cannot update a destroyed view, cannot leak the ViewModel, and cannot keep a network call alive after the user has left. Get the scope right and the leaks interviewers love to ask about simply cannot happen.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision