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

New Kid On The Block : Coroutines

New Kid On The Block : Coroutines

In the Android realm, we have always been in look out for the best solution for dealing with the asynchronous nature of the mobile world. From the infamous lifecycle handling to the hellish web of callbacks, we had seen it all. As the newest kid on the block: Kotlin Coroutines. We'll dive into what coroutines are, how they differ from other models and how to use it with Kotlin on Android. Using the power of Kotlin DSL, we will delegate the heavy lifting away from the regular android components.

Repo Link: https://github.com/DroidLove/AsyncDroid

JItesh SUvarna

October 13, 2018
Tweet

More Decks by JItesh SUvarna

Other Decks in Programming

Transcript

  1. • AsyncTask - Run short operations in sequence, also can

    be done concurrently Cons : Boilerplate code, Tedious cancellation and failure handling
  2. • Executors - Running tasks concurrently with fixed thread pool

    Cons : No direct access to UI thread, Needs micro managing
  3. • Intent service - Run long operations in sequence, handled

    by a queue Cons : No direct access to UI thread by default, Tasks are queued
  4. • RxJava - Using Observer and Observable for handling stream

    of data Cons : Steep learning curve, Mainly a event processing library
  5. What are Coroutines ? Coroutines are completely implemented through a

    compilation technique (no support from the VM or OS side is required), and suspension works through code transformation #DevfestMumbai
  6. Basic Components 1. Coroutine Builder - Launch  and Async 2.

    CoroutineScope - GlobalScope 3. CoroutineContext - Dispatchers.Main, Dispatchers.IO 4. CoroutineStart - DEFAULT and LAZY 5. Suspending Functions - Defines The function that does not block the thread. Use suspend modifiers to mark the function as suspended.
  7. Function A should be completed before Function B is executed.

    The thread is blocked for the Function B until Function A complete’s execution
  8. Function A is started, later it is suspend non -

    blocking and later let Function B execute. The thread is not blocked by Function A
  9. What are Coroutines now ? Kotlin coroutines allow to write

    asynchronous code in a familiar way, the structure of the code doesn’t change if you compare it with something synchronous. So you can think of coroutine as something that manages thread in a very efficient way. OR #DevfestMumbai
  10. LifeCycle of Coroutine • A Job interface is a cancellable

    thing with a life-cycle till its completion. • Jobs can be arranged into parent-child hierarchies where cancellation of parent lead to an immediate cancellation of all its children. • Exception Handling - CancellationException and try/catch block
  11. DSL in Kotlin • DSL - Domain Specific Language is

    a language that focuses on just one particular part of an application. • Using DSL, makes the overall quality of code to level up. • Can used in Configuration / Utility Class, Library Interface giving human readable syntax to the user.
  12. What about the Future ? • Coroutine in Kotlin is

    maintained under KEEP. [Kotlin Evolution and Enhancement Process] • From Koltin 1.3 RC - Coroutines are stable meaning that their API will not change in future releases. • Currently Coroutines have MultiPlatform Support for - JVM, JS and Kotlin - Native • Design documents, future improvement, issue/bug tracking • Usage with Anko, Retrofit Coroutine Adapter, Kotlin - Native
  13. thanks! Any questions? You can find me at @droidboy313 @ShaadiTech

    Repo Link : https://github.com/DroidLove/AsyncDroid #DevfestMumbai