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
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
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.
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.
Suspending Function 25 @rivuchakraborty https://www.rivu.dev ● suspend is a Keyword in Kotlin ● Compiler level restriction - can’t call suspend function outside CoroutineScope
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
Coroutine Scope 28 @rivuchakraborty https://www.rivu.dev ● Container of CoroutineContext ● Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext
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
Coroutine Context 31 @rivuchakraborty https://www.rivu.dev ● Holds key informations about the Coroutine such as Dispatcher / ThreadPool Configuration, Coroutine Name etc.