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.

36c29634c5d55eae66224c24ba2b933c?s=128

Rivu Chakraborty

November 25, 2018
Tweet

Transcript

  1. Rivu Chakraborty Efficient Async Coding in Kotlin Coroutines

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

  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-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

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

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

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

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

    same time • Not only for Android Developers • JavaScript - Promise • JVM, Android - Threads @rivuchakraborty #DevFestAhm
  10. 10 Concurrency in Android • AsyncTask • CompletableFuture • Runnable

    / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty #DevFestAhm
  11. 11 Concurrency in Android • AsyncTask @rivuchakraborty #DevFestAhm

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

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

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

  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.
  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).
  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.
  18. Coroutines 18 @rivuchakraborty #DevFestAhm Easy to Use

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

    & Dispatchers @rivuchakraborty #DevFestAhm
  20. Coroutines 20 @rivuchakraborty #DevFestAhm Suspending vs Blocking

  21. Coroutines 21 @rivuchakraborty #DevFestAhm Suspending vs Blocking

  22. Coroutines 22 @rivuchakraborty #DevFestAhm Suspending vs Blocking

  23. Launch a Coroutine 23 @rivuchakraborty #DevFestAhm

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

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

  26. Deferred 26 @rivuchakraborty #DevFestAhm • Deferred Extends Job • Wrapper

    around your data
  27. Suspending Function 27 @rivuchakraborty #DevFestAhm • suspend is a Keyword

    in Kotlin
  28. Suspending Function 28 @rivuchakraborty #DevFestAhm • suspend is a Keyword

    in Kotlin • Compiler level restriction - can’t call suspend function outside CoroutineScope
  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
  30. Coroutine Scope 30 @rivuchakraborty #DevFestAhm • Container of CoroutineContext

  31. Coroutine Scope 31 @rivuchakraborty #DevFestAhm • Container of CoroutineContext •

    Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
  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
  33. Coroutine Scope 33 @rivuchakraborty #DevFestAhm

  34. Dispatchers 34 @rivuchakraborty #DevFestAhm • Determines which Thread / ThreadPool,

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

  36. Repository 36 @rivuchakraborty #DevFestAhm

  37. API (Retrofit) 37 @rivuchakraborty #DevFestAhm

  38. Activity 38 @rivuchakraborty #DevFestAhm

  39. Call it 39 @rivuchakraborty #DevFestAhm

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

  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
  42. Rivu Chakraborty, BYJU’S @rivuchakraborty Thank you!