This is an introduction to Kotlin Coroutines and How we can use it in Android. I have tried cover all the basic stuff of coroutine and also added a comparison of thread and coroutine. Github sample project link: https://github.com/bipinvaylu/kotlin-coroutines
Slides with speaker contents: https://docs.google.com/presentation/d/1Wn9ImL9meON2Ohg7pPVYCXKNezilJyX6ewDWU3ljrwE/edit?usp=sharing
2. What is coroutines?
● Asynchronous task
● Light-Weight Thread
● Help to write code in synchronous manner
● Help to deal with complicated concurrent code
7. Reference
- Kotlin Coroutines in Android: https://sourcediving.com/kotlin-coroutines-in-
android-e2d5bb02c275
- KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov:
https://www.youtube.com/watch?v=_hfBv0a09Jc
- Guide to kotlinx.coroutines by example:
https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md
- Kotlin coroutines: interacting with external libraries and existing code:
https://medium.com/@andrea.bresolin/kotlin-coroutines-interacting-with-
external-libraries-and-existing-code-70001a835c30
Coroutine is just another way to do Asynchronous task.
Coroutine are essentially a light-weight alternative to threads.
It allows us to run asynchronous code in synchronous manner. That means we write code as like synchronous but it runs asynchronously
It help us the write clean code, that means we don’t need to deal with complicated concurrent code.
Coroutines has been introduced in kotlin 1.1. It’s still an experimental feature so, it’s likely to be changed.
Add kotlin coroutine dependency in app’s gradle file
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5"
Enable kotlin coroutine in gradle properties file
kotlin.coroutines=enable
Then start building suspend functions for concurrent code
A suspend function is just a regular Kotlin function with a additional `suspend` modifier indicate that the function can suspend the execution of coroutine.
Suspend function can call any other regular function to actually suspend the execution, It has to be another suspend function. That means, A suspend function cannot be invoked from a regular function.
Therefore, Coroutine builders are provided.
Coroutine builders are allow us to invoke suspend function from a regular non-suspending function/scope.
The Kotlin coroutine library provides several coroutines builders:
runBlocking: Launches a new coroutines and blocks current until its completion.
launch: Launches a new coroutines and return a reference to it as a Job object which doesn’t have a result value associated with it.
async: Launches a new coroutines and returns a reference to it as a Deferred<T> object which allows returning a value from it. It also store an exception. It has to be used together with await to suspend function which waits for that result without blocking the thread.
suspendCoroutine: This is special type of coroutine or you can callback wrapper coroutine. With suspendCoroutine() we can basically make the current coroutine suspend immediately. This method gives a continuation object of the coroutine. Using that we can manually control coroutine.
resume: We can return a value from suspend function in case of success.
resumeWithException: Some way, We can resume the coroutine with an exception in case of error.