Kotlin Coroutines: Fundamentals

Feb 14 2024 · Kotlin 1.9, Android 13, Android Studio Giraffe

Part 1: Getting started with Coroutines

01. Get an Overview of Android Threading Model

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Get Started with Kotlin Coroutines

Notes: 01. Get an Overview of Android Threading Model

Prerequisites: Basic Kotlin & Android knowledge

Transcript: 01. Get an Overview of Android Threading Model

Hello everyone! Welcome to the Kotlin Coroutines: Fundamentals course.

In this course, you’ll learn about the basics of threading, background processing and Kotlin Coroutines on Android.

These concepts are the bread and butter for nearly everything you’ll eventually learn about processing data in the apps. You’ll learn more about these topics in the coming slides.

So far, most of the time you worked with something called blocking function calls.

A blocking call is when you call a function, and you have to wait for it to finish, before you can move on to the rest of the code. These calls were synchronous in nature.

When you run a synchronous operation, you have to wait for its result, to proceed to the next operation.

But, functions and calls can also be non-blocking, or asynchronous. They can start after a small period of time, and can also run in parallel with other code.

This makes them non-blocking, as you don’t have to wait for their results when you call them. They can run in the background, while you do other things.

An example of a synchronous operation is a phone call. You have to actively listen to the person on the other side, and you have to stop whatever you’re doing, to respond, in order to have a healthy and optimal conversation.

If you’re trying to multitask, you’ll either lose out on information, as you won’t be actively listening, or you won’t speak fluently, as you’re also trying to focus on other things while trying to speak.

But you can for example work, and listen to music, at the same time. Your work is a blocking operation, synchronous, and you are actively executing it. You can’t do anything else which is also blocking while you’re working.

Listening to music in the background is asynchronous, non-blocking, and it doesn’t require of you to actively listen or pay attention.

To be able to support async work, and parallel execution, your applications use something called Threads.

Threads are like small robots which do some work within your apps. Each thread usually has a task it needs to work on. After that, it can switch to something else.

Threads take up a lot of resources, like memory. So you can’t have too many of them. You have to be careful when creating them, and distributing work between them.

Each Android app has one unique Main thread, also called the UI Thread, because it is primarily used to draw the user interface, like the buttons or text in the app.

The main thread needs to be fast, and you should avoid having long blocking calls on it. That would freeze the application or the UI, making it unresponsive.

If you’ve ever played a video game and it froze, or used an app, and it wasn’t responsive, that’s what happens when you block the main thread for too long!

Not a pretty sight! :]

To achieve non-blocking calls in Android, you use an approach called background processing.

Background processing is when you move an operation to a different thread, to keep the main thread as free as possible.

There are many different ways you can achieve non-blocking calls with Background processing, such as Services, WorkManagers, thread pools, and more.

And they are used in different scenarios.

For example services are good for long-running operations and WorkManager is good for work that should be done in specific conditions, like every day at 2:00 PM, when you’re connected to the Wi-Fi.

But sometimes, none of the native Android options seem like a good fit, for the use case you have.

This is where the concept of Kotlin Coroutines comes to the rescue!

A suspending function is a function that can pause and resume its execution. During the suspension period, the thread executing a function is free to do other tasks.

This means that you can write code that looks synchronous, but is actually asynchronous and non-blocking.

The function execution may resume with the same or another thread. So, you don’t have to worry about threads, and you can focus on writing clean and understandable code.

Kotlin Coroutines are a framework within the Kotlin language, that allows you to wrap your blocking calls, in non-blocking constructs named coroutines.

A coroutine is like a function, which by default runs asynchronously, in parallel.

However, unlike many other background processing concepts, they don’t require of you to write a lot of code, and completely change the way your code looks.

Coroutine code looks almost exactly like regular code, except that it is non-blocking.

This lets you write short, clean and understandable code, while still avoiding blocking calls!

To fully understand coroutines, and their benefits, you have to get a good understanding of threads, and how background processing works.

In the first part, you’ll learn about the basics of coroutines, threads, and suspending functions.

Everything you learn in this course will keep coming back to you in many other concepts you will learn within the Android ecosystem.

Things like working with Networking and Databases use Coroutines nowadays, and it’s important to understand why!