Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Efficient Async Coding in Kotlin Coroutines

Efficient Async Coding in Kotlin Coroutines

Presented in #DevFestAhm #DevFest18 #DevFestIndia
Working with asynchronous code is difficult especially for Android Developers which ultimately leads to callback hell. The only way out is using Rx, but again Rx has a really steep learning curve, and forces reactive style on the developers, whether you like it or not, the problem with Rx is that, most beginners in Rx tends to mix reactive style with imperative style which causes bugs and loopholes and also debugging Rx is almost a nightmare.
Most Android developers are using Kotlin nowadays, it has many fantastic features, Coroutines being most notable among them, moreover they're stable now.
This talk would cover:
- Co-routines Generator API Example (for explaining suspension)
- How and where to create Coroutine Scopes, and how to manage them
- How to make API calls just by using co-routines and Kotlin URL extension functions
- How to use co-routines with popular REST client Retrofit.

Rivu Chakraborty

November 25, 2018
Tweet

More Decks by Rivu Chakraborty

Other Decks in Programming

Transcript

  1. Rivu Chakraborty
    Efficient Async Coding in Kotlin
    Coroutines

    View Slide

  2. Rivu Chakraborty
    BYJU’S
    @rivuchakraborty
    Efficient Async Coding in
    Kotlin Coroutines

    View Slide

  3. Rivu Chakraborty
    BYJU’S
    @rivuchakraborty
    About Me
    ● Sr Software Engineer (Android) - BYJU’S
    ● Instructor - Caster.io
    ● Google Certified Associate Android Developer
    ● DroidJam Speaker
    ● Author - Reactive Programming in Kotlin
    ● Author - Functional Kotlin
    ● Author - Coroutines for Android Developers (WIP)

    View Slide

  4. @rivuchakraborty

    View Slide

  5. Books
    https://www.packtpub.com/application-d
    evelopment/reactive-programming-kotlin
    https://www.packtpub.com/application-de
    velopment/functional-kotlin
    https://leanpub.com/coroutines-for-andr
    oid-developers
    Work in Progress

    View Slide

  6. Efficient Async Coding in
    Kotlin Coroutines
    Async => Concurrency

    View Slide

  7. Concurrency
    ● Ability to execute
    multiple code blocks
    at the same time
    @rivuchakraborty
    #DevFestAhm

    View Slide

  8. Concurrency
    ● Ability to execute
    multiple code blocks at
    the same time
    ● Not only for Android
    Developers
    @rivuchakraborty
    #DevFestAhm

    View Slide

  9. Concurrency
    ● Ability to execute multiple
    code blocks at the same time
    ● Not only for Android
    Developers
    ● JavaScript - Promise
    ● JVM, Android - Threads
    @rivuchakraborty
    #DevFestAhm

    View Slide

  10. 10
    Concurrency in Android
    ● AsyncTask
    ● CompletableFuture
    ● Runnable / Thread (Custom Threadpool)
    ● Rx
    ● Counting...
    @rivuchakraborty
    #DevFestAhm

    View Slide

  11. 11
    Concurrency in Android
    ● AsyncTask
    @rivuchakraborty
    #DevFestAhm

    View Slide

  12. 12
    Concurrency in Android
    ● CompletableFuture
    @rivuchakraborty
    #DevFestAhm

    View Slide

  13. 13
    Concurrency in Android
    ● Runnable / Thread
    @rivuchakraborty
    #DevFestAhm

    View Slide

  14. 14
    Concurrency in Android
    ● Rx
    @rivuchakraborty
    #DevFestAhm

    View Slide

  15. 15
    Concurrency in Android
    ● Coroutines
    @rivuchakraborty
    #DevFestAhm
    Let’s you write non-blocking asynchronous code in your
    choice of Style - Sequentially, in Functional Style or whatever
    you prefer.

    View Slide

  16. Coroutines
    16
    @rivuchakraborty
    #DevFestAhm
    Light-Weight Threads
    Thread 1 Thread 2 Thread 3
    Coroutine 1 Coroutine 2 Coroutine 3
    With Coroutines, Threads are still used (for JVM).

    View Slide

  17. Coroutines
    17
    @rivuchakraborty
    #DevFestAhm
    Light-Weight Threads
    Thread 1 Thread 2 Thread 3
    Coroutine 1 Coroutine 2 C 3 C 4 C 5 C 6 C 7 C 8 C N
    But a single Coroutine can run multiple coroutines.

    View Slide

  18. Coroutines
    18
    @rivuchakraborty
    #DevFestAhm
    Easy to Use

    View Slide

  19. Suspension &
    Continuation
    Builders
    Suspending
    Functions
    End
    Coroutines
    Components
    Scope &
    Dispatchers
    @rivuchakraborty
    #DevFestAhm

    View Slide

  20. Coroutines
    20
    @rivuchakraborty
    #DevFestAhm
    Suspending vs
    Blocking

    View Slide

  21. Coroutines
    21
    @rivuchakraborty
    #DevFestAhm
    Suspending vs
    Blocking

    View Slide

  22. Coroutines
    22
    @rivuchakraborty
    #DevFestAhm
    Suspending vs
    Blocking

    View Slide

  23. Launch a Coroutine
    23
    @rivuchakraborty
    #DevFestAhm

    View Slide

  24. Get the Job done or cancel it, your
    choice. :)
    24
    @rivuchakraborty
    #DevFestAhm

    View Slide

  25. Compute Async(hrounously)
    25
    @rivuchakraborty
    #DevFestAhm

    View Slide

  26. Deferred
    26
    @rivuchakraborty
    #DevFestAhm
    ● Deferred
    Extends Job
    ● Wrapper
    around your
    data

    View Slide

  27. Suspending Function
    27
    @rivuchakraborty
    #DevFestAhm
    ● suspend is a
    Keyword in Kotlin

    View Slide

  28. Suspending Function
    28
    @rivuchakraborty
    #DevFestAhm
    ● suspend is a Keyword in
    Kotlin
    ● Compiler level
    restriction - can’t
    call suspend
    function outside
    CoroutineScope

    View Slide

  29. Suspending Function
    29
    @rivuchakraborty
    #DevFestAhm
    ● suspend is a Keyword in
    Kotlin
    ● Compiler level restriction -
    can’t call suspend function
    outside CoroutineScope
    ● Suspends
    execution of
    current coroutine

    View Slide

  30. Coroutine Scope
    30
    @rivuchakraborty
    #DevFestAhm
    ● Container of
    CoroutineContext

    View Slide

  31. Coroutine Scope
    31
    @rivuchakraborty
    #DevFestAhm
    ● Container of
    CoroutineContext
    ● Every Coroutine
    Builder is an
    extension over
    CoroutineScope,
    and thus inherits its
    CoroutineContext

    View Slide

  32. Coroutine Scope
    32
    @rivuchakraborty
    #DevFestAhm
    ● Container of
    CoroutineContext
    ● Every Coroutine Builder is an
    extension over
    CoroutineScope, and thus
    inherits its CoroutineContext
    ● All Coroutines must
    be launched within
    a CoroutineScope

    View Slide

  33. Coroutine Scope
    33
    @rivuchakraborty
    #DevFestAhm

    View Slide

  34. Dispatchers
    34
    @rivuchakraborty
    #DevFestAhm
    ● Determines which Thread / ThreadPool, the
    coroutine will run on.
    ● Similar to the Rx Schedulers

    View Slide

  35. Let’s Build for
    Android

    View Slide

  36. Repository
    36
    @rivuchakraborty
    #DevFestAhm

    View Slide

  37. API (Retrofit)
    37
    @rivuchakraborty
    #DevFestAhm

    View Slide

  38. Activity
    38
    @rivuchakraborty
    #DevFestAhm

    View Slide

  39. Call it
    39
    @rivuchakraborty
    #DevFestAhm

    View Slide

  40. Handle Exception (Gracefully) :)
    40
    @rivuchakraborty
    #DevFestAhm

    View Slide

  41. Rivu Chakraborty, BYJU’S
    @rivuchakraborty
    Resources
    https://caster.io/courses/kotlin-c
    oroutines-fundamentals
    ● http://bit.ly/CodeLabCoroutines
    ● http://bit.ly/CoroutinesAndroid
    ● http://bit.ly/DroidCoroutines
    ● http://bit.ly/CoroutinesGuide

    View Slide

  42. Rivu Chakraborty, BYJU’S
    @rivuchakraborty
    Thank you!

    View Slide