Version Third Edition
Platform Android 12
Language Kotlin 1.6
Editor Android Studio Bumblebee

The importance of concurrency is discovered quite early on by people who start with Android development. Android is inherently asynchronous and event-driven, with strict requirements as to on which thread certain things can happen.

Add to this the often-cumbersome Java callback interfaces, and you will be trapped in spaghetti code pretty quickly (aptly termed as “Callback Hell”). No matter how many coding patterns you use to avoid that, you will have to encounter the state change across multiple threads in one way or the other.

The only way to create a responsive app is by leaving the UI thread as free as possible, letting all the hard work be done asynchronously by background threads.

Kotlin Coroutines by Tutorials will teach you the techniques you need to solve common programming problems using asynchronous programming.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

iv

vi

Section I: Introduction to Coroutines

In the first chapter, you’ll learn about the problems related to multithreading and how coroutines can be an elegant solution. After setting up your development environment in IntelliJ or Android Studio, you’ll start writing your first coroutine to understand what suspending functions are and how to define them. You’ll finish this section learning how to use async and await functions for efficient use of resources.

In this very first chapter, you’ll learn what asynchronous programming means and why a modern developer should understand it. You’ll see the basics of multithreading like queue and shared memory, and you’ll understand how to solve the "Indentation Hell Problem."

1

Learning through example is one of the most efficient ways to gain more skills. To do this, you need to set up your build environment and learn how to load the starting projects with IntelliJ or Android Studio. This chapter describes all you need to start writing your code.

2

This is the chapter where you’ll learn the main concepts about coroutines like builders, scope and context. You’ll see for the first time the Job object and learn how to manage dependencies between coroutines. You’ll understand and write code to manage one of the most important features of asynchronous tasks: cancellations.

3

To understand how to use coroutines, you need to learn what a suspending function is and how to implement it. In this chapter, you’ll learn all you need to create and use your suspending functions. You’ll also learn how to change your existing functions to use them in a coroutine.

4

In multithreading and asynchronous development in Java, you often use Runnable, Callable and Future. With coroutines, you can use Deferred instead. These are objects that you can manage using the async/await functions. In this chapter, you’ll write code to understand when and how to use this pattern most effectively.

5

This chapter is about one of the most important concepts about coroutines: Coroutine Context. You'll learn what it is and how this is related to the dependencies between different coroutine jobs. You'll also learn how to create your context.

6

In this chapter, you'll learn how to run different Jobs into the proper thread. You'll learn how to configure and use the proper thread to display information on the UI or to invoke different services on the network.

7

Using functions with a callback is not difficult only because of the indentation hell problem but also for error and exception handling. In this very important chapter, you’ll learn, with several examples, all the techniques you can use to handle exceptions.

8

One of the most important topics to master when you deal with multithreading is cancellation. Starting a thread is very easy compared to the techniques used to cancel it, leaving the system in a consistent state. In this very important chapter, you’ll learn, with several examples, all the techniques you can use to manage cancellation.

9

Section II: Channels & Flows

Coroutines provide you a set of suspending functions and coroutine builders for the most common use cases. In order to use them, you need to know some more details on how they work under the hood. In this section, you’ll explore channels, Coroutine Flow and testing your coroutines.

Sequences are one of the most interesting features of Kotlin because they allow generating values lazily. When you implement a sequence you use the yield function which is a suspending function. In this chapter, you’ll learn how to create sequences and how the yield function can be used to optimize performance.

10

In this chapter, you'll learn what Coroutine Flow is and how to use it in your project.

11

In this chapter you'll learn everything you need about two specific Flow implementations which allow you to handle multiple collectors: SharedFlow and StateFlow.

12

Testing is a fundamental part of the development process and coroutines are not different. In this chapter, you'll learn how to test coroutines using the main testing frameworks.

13

Section III: Coroutines & Android

Coroutines are becoming a very important tool for any Kotlin application and in particular for Android applications. They allow the creation of applications that are more readable and use simpler code. In this section, you’ll learn how to use coroutines as a valid option for running background tasks which interact with the UI thread.

In Android, it's important to offload heavy work to a background thread, leaving the main thread free to do its work. In this chapter, you'll learn different ways to achieve this. Finally, you'll see how Coroutines can make this task nice and elegant.

14

A breakdown of the common problems when collecting flows in the Android UI. Introduction to lifecycleScope and examples of its usage.

15

Learn how you can utilize Kotlin Coroutines to make your networking code simple and performant. You'll explore several ways of doing API calls with Retrofit library and coroutines.

16

Learn how to use Room's built-in support for coroutines to make your database communication as simple as possible.

17

Learn about using coroutines inside ViewModels and make your functions safe to execute on the main thread. Compare LiveData to Kotlin Flow and learn how to observe flows from Jetpack Compose safely. See how to test coroutines on Android.

18

Meet the team

Contributors

Who is this book for

This book is for intermediate Kotlin or Android developers who already know the basics of UI development but want to learn coroutine API to simplify and optimize their code.

Concepts covered in this book

  • Asynchronous programming
  • Configuration
  • Coroutine principles
  • Suspending functions
  • Sequences & Iterators
  • Thread communication techniques

Reviews

Version history

Third Edition · Android 12, Kotlin 1.6, Android Studio Bumblebee

v3.0 · May 11 2022 · Luka Kordić & Filip Babić

Second Edition · Android 10, Kotlin 1.3, Android Studio 3.5

v2.0 · Sep 18 2019 · Nishant Srivastava & Filip Babić
See book details
Pro

Kotlin Coroutines by Tutorials

By Luka Kordić & Filip Babić

Android is inherently asynchronous and event-driven, with strict requirements as to on which thread certain things can happen. Learn how to use Kotlin coroutines to solve common Android programming problems using asynchronous programming techniques!

Read for Free with the Pro subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.