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 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)
  2. Concurrency • Ability to execute multiple code blocks at the

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

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

    / Thread (Custom Threadpool) • Rx • Counting... @rivuchakraborty #DevFestAhm
  5. 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.
  6. 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).
  7. 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.
  8. Get the Job done or cancel it, your choice. :)

    24 @rivuchakraborty #DevFestAhm
  9. Suspending Function 28 @rivuchakraborty #DevFestAhm • suspend is a Keyword

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

    Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
  12. 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
  13. Dispatchers 34 @rivuchakraborty #DevFestAhm • Determines which Thread / ThreadPool,

    the coroutine will run on. • Similar to the Rx Schedulers
  14. 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