Kotlin Coroutines

DEEP DIVE

This is a practical book. It shows everything with examples and focuses on real-life use cases. As this technology is already widely used on Android and backend applications, we will see how we can use it there and what the best practices are.

Introduction

Kotlin coroutines have revolutionized JVM development, especially on Android and the backend, as they let us easily implement efficient and reliable multithreading. Their cutting-edge design and features are ideally suited to modern use cases. In this book, we will explore how Kotlin coroutines work and how we can use them to improve our applications – using both the built-in support and the kotlinx.coroutines library.

Why coroutines?

The importance of concurrency is growing, but the classic techniques are not enough. Current trends suggest that coroutines are where our industry is clearly heading, and Kotlin coroutines are a very solid step in the right direction. Let me show them to you, with examples of how well they help in common use cases. I hope you will have a lot of fun reading this book.

Who this book is for

This book is for Kotlin developers who want to understand Kotlin Coroutines and learn to use them in real-life projects in accordance with best practices. The book focuses on backend and Android use cases, but it also presents knowledge that can be used on other platforms.

The structure of this book

The book is divided into the following parts:

  • Part 1: Understanding Kotlin Coroutines - dedicated to explaining what Kotlin Coroutines are and how they really work.
  • Part 2: Kotlin Coroutines library - explaining the most important concepts from the kotlinx.coroutines library and how to use them well.
  • Part 3: Channel and Flow - focused on Channel and Flow from the kotlinx.coroutines library.

What is covered

This book is based on a workshop I conduct. During its iterations I have been able to observe what interested attendees and what did not. These are the elements that are most often mentioned by attendees, so I mainly focus on the following:

  • How do coroutines really work? (Part 1)
  • How to use coroutines in practice? (Part 2 and 3)
  • What are the best practices? (Part 2 and 3)
  • Testing Kotlin coroutines (Testing Kotlin Coroutines in Part 2)
  • What is Flow and how does it work? (Part 3)
Marcin Moskała

Marcin Moskała

Marcin Moskala is an experienced developer and Kotlin trainer. He is the founder of the Kt. Academy, an official JetBrains partner for Kotlin training, author of the books Effective Kotlin, Kotlin Coroutines, Functional Kotlin and Android Development with Kotlin. He is also the main author of the biggest medium publication about Kotlin and a speaker invited to many programming conferences.

Chapters available as articles

Why using Kotlin Coroutines?

The explanation of why coroutines stand out and offer us what hasn't been offered by other technologies.

How does suspension work in Kotlin coroutines?

A deep explanation of how suspension works in Kotlin Coroutines.

Coroutines under the hood

A deep dive into how suspension and continuations work under the hood.

Coroutines built-in support vs library

What the difference is between the coroutine functionalities build into language, and those distributed as a library.

Coroutine builders

How do we start coroutines, what is structured concurrency and how does it work?

What is CoroutineContext and how does it work?

A deep explanation of the coroutine context - the powerful data holder for coroutines.

Cancellation in Kotlin Coroutines

Everything you need to know about the cancellation mechanism in Kotlin Coroutines.

Exception handling in Kotlin Coroutines

Everything you need to know about the exception handling mechanism in Kotlin Coroutines.

Coroutine scope functions

How coroutineScope, withContext and other scoping function work, and why it is great.

Kotlin Coroutines dispatchers

Where we should use each dispatcher from the Kotlin Coroutines library.

Constructing a coroutine scope

How we generally define coroutine scope on Android and on the backend.

Testing Kotlin Coroutines

How we test Kotlin Coroutines, including common Android and backend cases.

Flow building

Every flow needs to start somewhere, so let's explore all the different flow builders.

Flow lifecycle operations

Let's learn about lifecycle operations, controlling context, and handling exceptions in flow.

Combining flows: merge, zip, and combine

Let's learn how can we transform two flows into one, with merge, zip, and combine.

Collecting values on flow: fold and scan

Let's learn how can we accumulate flow values with fold and scan.

Flattening flow: flatMapConcat, flatMapMerge and flatMapLatest

Let's learn how can we flatMap flow with flatMapConcat, flatMapMerge, and flatMapLatest.

SharedFlow and StateFlow

Known as a replacement for Subject, LiveData, and many more. SharedFlow and StateFlow are powerful coroutines classes, every Kotlin developer should know.

Kotlin Coroutines use cases for Data/Adapters Layer

How do we use Kotlin Coroutines in the Data/Adapters Layer, how do we use callback or blocking functions.

Kotlin Coroutines use cases for Domain Layer

How do we use Kotlin Coroutines in the Domain Layer.

Kotlin Coroutines use cases for Presentation/API/UI Layer

How do we use Kotlin Coroutines in the Presentation/API/UI Layer Layer, how do we create scope and start coroutines.