Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Mark L. Murphy

Rating No ratings yet

No description

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, example-driven guide to Kotlin coroutines that takes you from "what is a coroutine?" to confidently structuring concurrent work with builders, jobs, flows, and channels. Best for Kotlin developers (especially Android) who want to write correct asynchronous code without callback hell. 【Book Arc】 - **Opening (~0%–10%)**: Frames the problem — asynchronous work and "callback hell" — and introduces coroutines as the solution, including the basic vocabulary of scope, builder, dispatcher, and suspend function. - **Early (~10%–32%)**: Covers the core building blocks: coroutine builders (`launch()`, `async()`), dispatchers and their defaults, `CoroutineScope` (including why `GlobalScope` is a code smell), and the fundamentals of `Job` management. - **Middle (~32%–55%)**: Deepens job control — lazy start, job states, cancellation (including cooperative cancellation, `cancelAndJoin()`, `NonCancellable`, and yielding), plus deferring results with `Deferred`. - **Late (~55%–80%)**: Moves into data streams: `Flow` (creation, consumption, dispatchers, operators, exceptions, `retry()`/`retryWhen()`), channels (send/receive, capacity, broadcasting), and `StateFlow`. - **Ending (~80%–100%)**: Bridges coroutines to existing callback-based APIs and shows how to create custom scopes, tying the pieces together for real-world integration. 【Key Takeaways】 - **Coroutines solve callback hell by letting you write sequential-looking async code** (Opening): suspend functions pause without blocking threads, so the same code reads top-to-bottom while running concurrently. - **Builders define intent: `launch()` for fire-and-forget, `async()` for results** (Early): `launch()` returns a `Job` for management; `async()` returns a `Deferred` whose `await()` yields the value. - **Dispatchers decide where code runs, and defaults are not always obvious** (Early): `Dispatchers.Default` is the usual default, but scopes like Android's `viewModelScope` use `Dispatchers.Main` — declaring your dispatcher explicitly is safest and most self-documenting. - **`GlobalScope` is a code smell in production** (Early): it lives for the whole process and is easy to misuse; prefer a focused `CoroutineScope` tied to a component's lifecycle. - **Cancellation is cooperative, not automatic** (Middle): a canceled job only stops when code checks in (e.g., `delay()`, `yield()`, or `withContext`); blocking loops like a busy-wait ignore cancellation entirely. - **`NonCancellable` lets cleanup finish after cancellation** (Middle): wrapping a `finally` block in `withContext(NonCancellable)` ensures essential cleanup runs even when the job is canceled. - **Flows are cold streams; channels are hot** (Late): `Flow` emits on collection and supports operators like `retry()`/`retryWhen()` for transient failures; channels support send/receive with capacity planning and broadcasting. - **`StateFlow` is the modern state-holder** (Late): it replaces `ConflatedBroadcastChannel` and compares favorably to `LiveData`, though it has its own limitations worth understanding. 【Reading Tips】 - **Deep-read the builders, jobs, and cancellation chapters** (Early–Middle): these are the foundation; skimming them will make later flow/channel material harder to follow. - **Skim the introductory callback-hell framing** (Opening) if you already know why async is hard — get to the coroutine mechanics quickly. - **Run the Klassbook samples as you go**: the book leans on executable snippets, and seeing output (especially for concurrency ordering) cements the mental model. - **Pay special attention to cancellation and dispatcher sections**: these are the most common sources of production bugs and the hardest spots to get right. - **Treat flows, channels, and `StateFlow` as a toolkit to compare**, not memorize — note when each is appropriate rather than learning every operator. 【Coverage Limits】 This guide is based on stratified excerpts covering the table of contents, preface, and selected chapters; some chapters (e.g., deferring results, structuring concurrency, custom scopes) are only partially represented, so specific details there may be understated.
Page 5
........................................................ 95 ◦ Using a SendChannel ................................................................ 99 ◦ Consu...
View in text
Excerpt 2
call, it knows that it can suspend execution of that first coroutine, if desired. In the sequential example, we only have one coroutine, so Kotlin just waits...
View in text
Excerpt 3
delay(2000L) } } (from "Lazy Coroutines" in the Klassbook) If you run this sample, you will not see “This is executed after the delay”, because that coroutin...
View in text
Excerpt 4
hing) • Returns the emitted object for a single-object Flow • Throws an exception if the Flow does not close after emitting its first object However, singleO...
View in text
Excerpt 5
t means it is always safe to get the value from a StateFlow. value can be of type T, not T?, which would force you into a never- ending set of safe calls or...
View in text
Excerpt 6
ce, gridX, gridY) val current = _results.value if (current !is MainViewState.Content || current.scenario != scenario) { _results.value = MainViewState.Loadin...
View in text
Excerpt 7
tor = AsyncTask.THREAD_POOL_EXECUTOR, post: (T) -> Unit 166 APPLYING COROUTINES TO YOUR UI val onChange = object : View.OnLayoutChangeListener { override fun...
View in text
Excerpt 8
y the main code of the motor. The “content” for the Content state consists of a list of RowState objects, where each RowState is a formatted rendition of the...
View in text
Tags
AI categories
Programming LanguageGoBackend
Publish Year: 2020
Language: English
File Format: PDF
File Size: 1.3 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…