Presentation Notes
Coroutines Presentation
This presentation should take around 45 minutes to complete, without questions.
This has been presented to Well Android Team on two parts, on Jan 9 and Jan 16, 2025, respectively.
Today I would like to talk about coroutines.
This is a very important subject since we are using coroutines throughout our app. Actually not only this app, you have probably used it in your previous project as well.
Coroutines are not unique to Kotlin. Similar concept can be found in various languages like C#, Python, Swift, Javascript, C and many more. Kotlin has introduced coroutines in version 1.3. Android has adopted and extended it and most of the Jetpack libraries are using coroutines and has extension functions.
I will talk about why are we using coroutines, the subjects around coroutines and some under the hood implementations.
So what is coroutines? The first thing I can say is that coroutines are not threads.
A coroutine is an instance of a suspendable computation. It is conceptually similar to a thread, like it takes a block of code to run that works concurrently with the rest of the code. However, a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume in another one. (source)
When compared to the threads, coroutines are really lightweight.

Will work fine

Out of memory error
It may not be realistic to run fifty thousand code blocks in parallel, but this example shows us using coroutines is more performant and reliable.
Why are we using coroutines? If you are developing a fairly moderate app, pretty sure you will need concurrency. You will want to execute some code after some expensive operation, or some time consuming operation like network request has completed.
You can achieve concurrency by yourself tho, using callbacks.

In this example, we would like to load movies, but before loading them, we would like to log the user in. We can implement this logic by using callbacks in this not complex example it's still not easy to understand what is going on just by looking at the code. It will probably get worse when we want to add error handling. Take a look at http://callbackhell.com.