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

36c29634c5d55eae66224c24ba2b933c?s=128

Rivu Chakraborty

March 23, 2019
Tweet

Transcript

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

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

  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
  4. https://www.rivu.dev

  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
  6. Coroutines & Asynchronous Programming Async => Concurrency

  7. Coroutines & Asynchronous Programming Concurrency ⇏ Parallel

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

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

    same time • Not only for Android Developers @rivuchakraborty https://www.rivu.dev
  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
  11. 11 Concurrency in Android • AsyncTask • CompletableFuture • Runnable

    / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty https://www.rivu.dev
  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.
  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).
  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.
  15. Coroutines 15 @rivuchakraborty https://www.rivu.dev Easy to Use

  16. Suspension & Continuation Builders Suspending Functions Done Coroutines Components Scope

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

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

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

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

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

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

  23. Deferred 23 @rivuchakraborty https://www.rivu.dev • Deferred Extends Job • Wrapper

    around your data
  24. Suspending Function 24 @rivuchakraborty https://www.rivu.dev • suspend is a Keyword

    in Kotlin
  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
  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
  27. Coroutine Scope 27 @rivuchakraborty https://www.rivu.dev • Container of CoroutineContext

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

  31. Coroutine Context 31 @rivuchakraborty https://www.rivu.dev • Holds key informations about

    the Coroutine such as Dispatcher / ThreadPool Configuration, Coroutine Name etc.
  32. Dispatchers 32 @rivuchakraborty https://www.rivu.dev • Determines which Thread / ThreadPool,

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

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

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

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

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

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

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

  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
  41. Thank you! 41 Rivu Chakraborty, BYJU’S @rivuchakraborty https://www.rivu.dev