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

Efficient Async Coding in Kotlin Coroutines - BoJUG

Efficient Async Coding in Kotlin Coroutines - BoJUG

Presented at BoJUG, January 2019 conference, this presentation walks you through the basics of Coroutines and guides you in implementing them in your project.

36c29634c5d55eae66224c24ba2b933c?s=128

Rivu Chakraborty

January 19, 2019
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Rivu Chakraborty Efficient Async Coding in Kotlin Coroutines 1 Bangalore

    JAVA User Group (BoJUG) - Year’s First Meetup - Full Day - Informatica - Jan, 19, 2019 19/01/19
  2. Rivu Chakraborty BYJU’S @rivuchakraborty Efficient Async Coding in Kotlin Coroutines

    2
  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)
  4. @rivuchakraborty

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

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

  7. Concurrency • Ability to execute multiple code blocks at the

    same time @rivuchakraborty
  8. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers @rivuchakraborty
  9. Concurrency • Ability to execute multiple code blocks at the

    same time • Not only for Android Developers • JavaScript - Promise • JVM, Android - Threads @rivuchakraborty
  10. 10 Concurrency in Android and/or JVM • AsyncTask (Android only)

    • CompletableFuture (Android - API 24+, JVM 1.8) • Runnable / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty
  11. 11 Concurrency in Android • Runnable / Thread @rivuchakraborty

  12. 12 Concurrency in Android • Rx @rivuchakraborty

  13. 13 Concurrency in Android • Rx @rivuchakraborty If you’re using

    Rx only for concurrency, sorry pal, you’re doing it wrong.
  14. 14 • Coroutines @rivuchakraborty Let’s you write non-blocking asynchronous code

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

    in your choice of Style - Sequentially, in Functional Style or whatever you prefer. A Language level feature, managed by the Kotlin team itself.
  16. 16 • Coroutines @rivuchakraborty • Let’s you write non-blocking asynchronous

    code in your choice of Style - Sequentially, in Functional Style or whatever you prefer. • A Language level feature, managed by the Kotlin team itself. You can add Coroutines dependency to your Kotlin project by following the instructions in GitHub ReadMe - https://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#u sing-in-your-projects
  17. Coroutines 17 @rivuchakraborty Light-Weight Threads Thread 1 Thread 2 Thread

    3 Coroutine 1 Coroutine 2 Coroutine 3 With Coroutines, Threads are still used (for JVM).
  18. Coroutines 18 @rivuchakraborty 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 can run multiple coroutines.
  19. Coroutines 19 @rivuchakraborty Easy to Use

  20. Suspension & Continuation Builders Suspending Functions End Coroutines Components Scope

    & Dispatchers @rivuchakraborty
  21. Coroutines 21 @rivuchakraborty Suspending vs Blocking

  22. Coroutines 22 @rivuchakraborty Suspending vs Blocking

  23. Coroutines 23 @rivuchakraborty Suspending vs Blocking

  24. Coroutines 24 @rivuchakraborty Suspending vs Blocking

  25. Launch a Coroutine 25 @rivuchakraborty

  26. Get the Job done or cancel it, your choice. :)

    26 @rivuchakraborty
  27. Compute Async(hrounously) 27 @rivuchakraborty

  28. Deferred 28 @rivuchakraborty • Deferred Extends Job • Wrapper around

    your data
  29. Suspending Function 29 @rivuchakraborty • suspend is a Keyword in

    Kotlin
  30. Suspending Function 30 @rivuchakraborty • suspend is a Keyword in

    Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope
  31. Suspending Function 31 @rivuchakraborty • suspend is a Keyword in

    Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope • Suspends execution of current coroutine
  32. Coroutine Scope 32 @rivuchakraborty • Container of CoroutineContext

  33. Coroutine Scope 33 @rivuchakraborty • Container of CoroutineContext • Every

    Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
  34. Coroutine Scope 34 @rivuchakraborty • Container of CoroutineContext • Every

    Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext • All Coroutines must be launched within a CoroutineScope
  35. Coroutine Scope 35 @rivuchakraborty

  36. Dispatchers 36 @rivuchakraborty • Determines which Thread / ThreadPool, the

    coroutine will run on. • Similar to the Rx Schedulers
  37. Let’s Build for Android

  38. Repository 38 @rivuchakraborty

  39. API (Retrofit) 39 @rivuchakraborty

  40. Activity 40 @rivuchakraborty

  41. Call it 41 @rivuchakraborty

  42. Handle Exception (Gracefully) :) 42 @rivuchakraborty

  43. Rivu Chakraborty @rivuchakraborty Resources https://caster.io/courses/kotlin-coroutines-fundamental s • http://bit.ly/CodeLabCoroutines (Codelab) •

    http://bit.ly/CoroutinesAndroid (Article) • http://bit.ly/DroidCoroutines (Book, yet to be published) • http://bit.ly/CoroutinesGuide (Official Guide) • https://www.meetup.com/BlrKotlin/ (Bangalore Kotlin User Group) 43
  44. Rivu Chakraborty @rivuchakraborty Thank you! 44