$30 off During Our Annual Pro Sale. View Details »

Coroutines and Asynchronous Programming (BlrKotlin)

Coroutines and Asynchronous Programming (BlrKotlin)

Presented at BlrKotlin March Meetup (2019), this talks guids you through the basic concepts of Coroutines and how to use them for you asynchronous programming requirement in Android

Rivu Chakraborty

March 23, 2019
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Rivu Chakraborty
    Coroutines & Asynchronous
    Programming
    1
    https://www.rivu.dev

    View Slide

  2. Rivu Chakraborty
    BYJU’S
    @rivuchakraborty
    Coroutines & Asynchronous
    Programming
    2
    https://www.rivu.dev

    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 - Hands on Data Structures and Algorithms
    with Kotlin
    ● Author - Coroutines for Android Developers (WIP)
    3
    https://www.rivu.dev

    View Slide

  4. https://www.rivu.dev

    View Slide

  5. Books
    https://www.packtpub.com/a
    pplication-development/react
    ive-programming-kotlin
    https://www.packtpub.com/a
    pplication-development/funct
    ional-kotlin
    https://leanpub.com/coroutines-f
    or-android-developers
    Work in Progress
    https://www.packtpub.com/appl
    ication-development/hands-dat
    a-structures-and-algorithms-ko
    tlin

    View Slide

  6. Coroutines & Asynchronous
    Programming
    Async => Concurrency

    View Slide

  7. Coroutines & Asynchronous
    Programming
    Concurrency ⇏ Parallel

    View Slide

  8. Concurrency
    ● Ability to execute
    multiple code blocks
    at the same time
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  9. Concurrency
    ● Ability to execute
    multiple code blocks at
    the same time
    ● Not only for Android
    Developers
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  10. Concurrency
    ● Ability to execute multiple code
    blocks at the same time
    ● Not only for Android Developers
    ● JavaScript - Promise
    ● JVM, Android - Threads
    (Fibers)
    ● Native (LLVM) - Pure Kotlin
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  11. 11
    Concurrency in Android
    ● AsyncTask
    ● CompletableFuture
    ● Runnable / Thread (Custom Threadpool)
    ● Rx
    ● Counting...
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  12. 12
    Concurrency in Android
    ● Coroutines
    @rivuchakraborty
    https://www.rivu.dev
    Let’s you write non-blocking asynchronous code in your
    choice of Style - Sequentially, in Functional Style or whatever
    you prefer.

    View Slide

  13. Coroutines
    13
    @rivuchakraborty
    https://www.rivu.dev
    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

  14. Coroutines
    14
    @rivuchakraborty
    https://www.rivu.dev
    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 Thread (Fiber) can run multiple
    coroutines.

    View Slide

  15. Coroutines
    15
    @rivuchakraborty
    https://www.rivu.dev
    Easy to Use

    View Slide

  16. Suspension &
    Continuation
    Builders
    Suspending
    Functions
    Done
    Coroutines
    Components
    Scope &
    Dispatchers
    @rivuchakraborty
    https://www.rivu.dev
    16

    View Slide

  17. Coroutines
    17
    @rivuchakraborty
    https://www.rivu.dev
    Suspending vs
    Blocking

    View Slide

  18. 18
    @rivuchakraborty
    https://www.rivu.dev
    Suspending vs
    Blocking

    View Slide

  19. 19
    @rivuchakraborty
    https://www.rivu.dev
    Suspending vs
    Blocking

    View Slide

  20. Launch a Coroutine
    20
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  21. Get the Job done or cancel it,
    your choice. :)
    21
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  22. Compute Async(hrounously)
    22
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  23. Deferred
    23
    @rivuchakraborty
    https://www.rivu.dev
    ● Deferred
    Extends Job
    ● Wrapper
    around your
    data

    View Slide

  24. Suspending Function
    24
    @rivuchakraborty
    https://www.rivu.dev
    ● suspend is a
    Keyword in Kotlin

    View Slide

  25. Suspending Function
    25
    @rivuchakraborty
    https://www.rivu.dev
    ● suspend is a Keyword in
    Kotlin
    ● Compiler level
    restriction - can’t
    call suspend
    function outside
    CoroutineScope

    View Slide

  26. Suspending Function
    26
    @rivuchakraborty
    https://www.rivu.dev
    ● suspend is a Keyword in
    Kotlin
    ● Compiler level restriction -
    can’t call suspend function
    outside CoroutineScope
    ● Suspends
    execution of
    current coroutine,
    until the function is
    completed

    View Slide

  27. Coroutine Scope
    27
    @rivuchakraborty
    https://www.rivu.dev
    ● Container of
    CoroutineContext

    View Slide

  28. Coroutine Scope
    28
    @rivuchakraborty
    https://www.rivu.dev
    ● Container of
    CoroutineContext
    ● Every Coroutine
    Builder is an
    extension over
    CoroutineScope,
    and thus inherits its
    CoroutineContext

    View Slide

  29. Coroutine Scope
    29
    @rivuchakraborty
    https://www.rivu.dev
    ● 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

  30. Coroutine Scope
    30
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  31. Coroutine Context
    31
    @rivuchakraborty
    https://www.rivu.dev
    ● Holds key
    informations about
    the Coroutine such
    as Dispatcher /
    ThreadPool
    Configuration,
    Coroutine Name
    etc.

    View Slide

  32. Dispatchers
    32
    @rivuchakraborty
    https://www.rivu.dev
    ● Determines which Thread / ThreadPool, the
    coroutine will run on.

    View Slide

  33. Dispatchers
    33
    @rivuchakraborty
    https://www.rivu.dev
    ● Determines which Thread / ThreadPool, the coroutine will run on.
    ● Similar to the Rx Schedulers

    View Slide

  34. Let’s Build for
    Android

    View Slide

  35. Repository
    35
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  36. API (Retrofit)
    36
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  37. Activity
    37
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  38. Call it
    38
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  39. Handle Exception (Gracefully) :D
    39
    @rivuchakraborty
    https://www.rivu.dev

    View Slide

  40. Rivu Chakraborty, BYJU’S
    @rivuchakraborty
    https://www.rivu.dev
    Resources
    https://caster.io/courses/kotlin-co
    routines-fundamentals
    ● http://bit.ly/CodeLabCoroutines
    ● http://bit.ly/CoroutinesAndroid
    ● http://bit.ly/DroidCoroutines
    ● http://bit.ly/CoroutinesGuide
    40

    View Slide

  41. Thank you!
    41
    Rivu Chakraborty, BYJU’S
    @rivuchakraborty
    https://www.rivu.dev

    View Slide