KEMBAR78
Android kotlin coroutines | PPTX
What is coroutines?
● Asynchronous task
● Light-Weight Thread
● Help to write code in synchronous manner
● Help to deal with complicated concurrent code
Coroutines Implementation
● Add dependency
● Enable coroutines
● Build suspend function
Suspend Function?
● Suspend modifier
● Call from suspend function only
● Example:
suspend fun postData() {
// Post data to the server
}
Coroutines Builder
● runBlocking
● launch
● Async
○ await
○ Deferred<T>
○ Deferred<String>
● suspendCoroutine
○ resume
○ resumeWithException
Let’s do a code
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
Questions?
Thank you!

Android kotlin coroutines

  • 2.
    What is coroutines? ●Asynchronous task ● Light-Weight Thread ● Help to write code in synchronous manner ● Help to deal with complicated concurrent code
  • 3.
    Coroutines Implementation ● Adddependency ● Enable coroutines ● Build suspend function
  • 4.
    Suspend Function? ● Suspendmodifier ● Call from suspend function only ● Example: suspend fun postData() { // Post data to the server }
  • 5.
    Coroutines Builder ● runBlocking ●launch ● Async ○ await ○ Deferred<T> ○ Deferred<String> ● suspendCoroutine ○ resume ○ resumeWithException
  • 6.
  • 7.
    Reference - Kotlin Coroutinesin 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
  • 8.

Editor's Notes

  • #3 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.
  • #4 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
  • #5 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.
  • #6 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.
  • #7 Code: https://github.com/bipinvaylu/kotlin-coroutines