From the course: Complete Guide to Android Development with Kotlin for Beginners

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Execute code asynchronously with Kotlin Coroutines

Execute code asynchronously with Kotlin Coroutines

- [Instructor] Kotlin's coroutines allow you to handle asynchronous tasks in your applications. This is very important on Android as we need to avoid blocking the main thread so we don't hinder the usability of our app. The main thread on Android is a single thread that handles all updates to the UI. It's also the thread that handles click handlers and other UI callbacks. Think of it like an assembly line. Work is processed one after the other. But when a long-running task is performed on the main thread, it blocks the rest of the UI work from taking place. This can make your app pause or even freeze. Coroutines is Kotlin's solution to handling long-running tasks that might block the main thread. It's also the official recommendation from Google. Coroutines let you write code in a linear style. This type of code is simple to reason about. In this example, we're launching a coroutine that will perform its operations on the IO thread, and then we wait for the results before we continue…

Contents