Book Image

Simplifying Android Development with Coroutines and Flows

By : Jomar Tigcal
Book Image

Simplifying Android Development with Coroutines and Flows

By: Jomar Tigcal

Overview of this book

Coroutines and flows are the new recommended way for developers to carry out asynchronous programming in Android using simple, modern, and testable code. This book will teach you how coroutines and flows work and how to use them in building Android applications, along with helping you to develop modern Android applications with asynchronous programming using real data. The book begins by showing you how to create and handle Kotlin coroutines on Android. You’ll explore asynchronous programming in Kotlin, and understand how to test Kotlin coroutines. Next, you'll learn about Kotlin flows on Android, and have a closer look at using Kotlin flows by getting to grips with handling flow cancellations and exceptions and testing the flows. By the end of this book, you'll have the skills you need to build high-quality and maintainable Android applications using coroutines and flows.
Table of Contents (11 chapters)
1
Part 1 – Kotlin Coroutines on Android
6
Part 2 – Kotlin Flows on Android

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “runOnUIThread will perform the displayText(text) function on the main UI thread.”

A block of code is set as follows:

lifecycleScope.launch(Dispatchers.IO) {
     val fetchedText = fetchText()
       
     withContext(Dispatchers.Main) {
           displayText(fetchedText)
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

private fun fetchTextWithThread() {
     Thread {
          // get text from network
          val text = getTextFromNetwork()
           runOnUiThread {
                // Display on UI
                displayText(text)
           }
      }.start()
}

Any command-line input or output is written as follows:

java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “In Android Studio, the Editor window identifies the suspending function calls in your code with a gutter icon next to the line number.”

Tips or Important Notes

Appear like this.