Welcome to 2nd issue of the Engineering Insights newsletter! Today’s issue is about Kotlin Coroutines.
Kotlin Coroutines provides a lightweight way to perform multiple tasks concurrently, making it easy to write asynchronous, non-blocking code. Coroutines are also widely used in Android development.
Below you can find many resources grouped by topic to improve your knowledge of Kotlin Coroutines.
📝 What are Coroutines?
Introduction to Kotlin Coroutines for Beginners
Introductory article that mentions many Coroutines concepts but doesn’t go deep into any of them. Good if you’re just getting started.
Threads vs Coroutines in Kotlin
If you’re new to Coroutines you might be wondering how they differ from threads. This brief article lists the differences and the benefits of using Coroutines.
▶️ Main Concepts
Coroutines (Part I) – Grasping the Fundamentals
First article of a 3 part series explaining how Coroutines work. Really good if you want to deepen your understanding of Coroutines. Talks about suspend, main-safety and many other subjects.
Kotlin Coroutines: The Suspend Function
Explains how the suspend keyword works under the hood.
🎨 CoroutineContext
Coroutine context and dispatchers
The article talks about CoroutineContext
and Dispatcher and how they interact with each other.
What is the CoroutineContext?
The article talks about how CoroutineContext
works.
🧪 CoroutineScope
Coroutine Context and Scope
Every coroutine in Kotlin has a context that is represented by an instance of
CoroutineContext
interface. A context is a set of elements and current coroutine context is available viacoroutineContext
property
📧 CoroutineDispatcher
Coroutine Context and Dispatchers
CoroutineContext is immutable, but we can have a new context by adding an element, removing one, or merging two existing contexts.
CoroutineDispatcher is a subtype of the ContinuationInterceptor element of context. Therefore, it is responsible for determining the execution thread (or threads) of the coroutine.
🔨 Job/Supervisor Job
Coroutines (Part II) – Job, SupervisorJob, Launch and Async
We’ll start by learning about
Job
andSupervisorJob
and then proceed to creating Coroutines withlaunch
andasync
.
Kotlin Coroutine Job Lifecycle
[…] I want to go deep into the topic of Coroutine Scope.
Job vs SupervisorJob in coroutines/Kotlin
Jobs can be arranged in parent-child hierarchies. So, parent-child relation of coroutines is based on this coroutine context element.
Cancellation of parent Job results in cancellation of all its children
Failing of one child, without CancellationException, results in failing the parent Job which further causes to fail other children too.
🗙 Handling Exceptions and Cancellation
Coroutines (Part III) – Structured Concurrency and Cancellation
The author talks about how to handle cancellation in Coroutines and also teaches about the CoroutineExceptionHandler.
Are You Handling Exceptions in Kotlin Coroutines Properly?
You might also think that handling these exceptions is as easy as in normal Kotlin or Java code. Unfortunately, when we start using nested execution, things might start working not quite as we would expect.
Cancellation in coroutines
You need to make sure that you control the life of the coroutine and cancel it when it’s no longer needed — this is what structured concurrency represents. Read on to find out the ins and outs of coroutine cancellation.
Things I Misunderstood About Kotlin Coroutine Cancellations and Exceptions
Cancellations and Exceptions in Coroutines don’t work exactly as they work for normal code. The author shares some mistakes they made so you can avoid them.
🏗️ Structured Concurrency
Structured concurrency
This is a topic you need to understand well otherwise your coroutines will work in some unexpected ways.
🧪 Testing
Kotlin coroutine unit testing the better way
The author teaches how to use MockK with Coroutines.
📱 Integration with Android
Uses of Coroutines in Android
Mentions many important concepts including main safety.
Easy Coroutines in Android: viewModelScope
Explains more in depth how the viewModelScope works, how to use it and how to test it.
Guide to UI programming with coroutines
Guide about Coroutines that mentions some topics like actors that are often not mentioned in other articles.
Coroutines on Android (part I): Getting the background
This post focuses on how coroutines work and what problems they solve.
Best practices for coroutines in Android
Official Android article that lists many good practices, most of them are related to Android but can also be applied without Android.
Using Kotlin Coroutine builders in Android
Gives introduction about runBlocking
, launch
and async
.
🥇 Coroutines best practices
The reason to avoid GlobalScope
It seems that using
GlobalScope
is a good default for launching work in background, however we do not recommend usingGlobalScope
. Why? Let us see it with an example.
Coroutines & Patterns for work that shouldn’t be cancelled
There are cases when you want an operation to complete even if the user navigated away from a screen. As such, you don’t want the work to be cancelled.
Kotlin Coroutines patterns & anti-patterns
The author lists 6 do’s and dont’s of coroutines. Talks about GlobalScope, await and other topics.
This is the 2nd of many issues of my new newsletter aimed at helping you become a better engineer. It’ll be focused on Android/Kotlin but will also have topics that apply to software engineering in general.
Instead of simply sharing the latest news and updates, I've curated a selection of articles/videos that we believe offer unique insights and have the potential to make a significant impact on your work as a developer.
If you like this issue please share and subscribe. See you next time :)
Photo by Richard Horvath on Unsplash