Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

https://www.rivu.dev

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Coroutines & Asynchronous Programming Async => Concurrency

Slide 7

Slide 7 text

Coroutines & Asynchronous Programming Concurrency ⇏ Parallel

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Concurrency ● Ability to execute multiple code blocks at the same time ● Not only for Android Developers @rivuchakraborty https://www.rivu.dev

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

11 Concurrency in Android ● AsyncTask ● CompletableFuture ● Runnable / Thread (Custom Threadpool) ● Rx ● Counting... @rivuchakraborty https://www.rivu.dev

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

Coroutines 15 @rivuchakraborty https://www.rivu.dev Easy to Use

Slide 16

Slide 16 text

Suspension & Continuation Builders Suspending Functions Done Coroutines Components Scope & Dispatchers @rivuchakraborty https://www.rivu.dev 16

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Get the Job done or cancel it, your choice. :) 21 @rivuchakraborty https://www.rivu.dev

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Deferred 23 @rivuchakraborty https://www.rivu.dev ● Deferred Extends Job ● Wrapper around your data

Slide 24

Slide 24 text

Suspending Function 24 @rivuchakraborty https://www.rivu.dev ● suspend is a Keyword in Kotlin

Slide 25

Slide 25 text

Suspending Function 25 @rivuchakraborty https://www.rivu.dev ● suspend is a Keyword in Kotlin ● Compiler level restriction - can’t call suspend function outside CoroutineScope

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Coroutine Scope 27 @rivuchakraborty https://www.rivu.dev ● Container of CoroutineContext

Slide 28

Slide 28 text

Coroutine Scope 28 @rivuchakraborty https://www.rivu.dev ● Container of CoroutineContext ● Every Coroutine Builder is an extension over CoroutineScope, and thus inherits its CoroutineContext

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Coroutine Scope 30 @rivuchakraborty https://www.rivu.dev

Slide 31

Slide 31 text

Coroutine Context 31 @rivuchakraborty https://www.rivu.dev ● Holds key informations about the Coroutine such as Dispatcher / ThreadPool Configuration, Coroutine Name etc.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Dispatchers 33 @rivuchakraborty https://www.rivu.dev ● Determines which Thread / ThreadPool, the coroutine will run on. ● Similar to the Rx Schedulers

Slide 34

Slide 34 text

Let’s Build for Android

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Thank you! 41 Rivu Chakraborty, BYJU’S @rivuchakraborty https://www.rivu.dev