Slide 1

Slide 1 text

Bottom Up View of Coroutines Rooparsh Kalia SDE 2 at Mutual Mobile @i_am_rooparsh

Slide 2

Slide 2 text

Synchronous way

Slide 3

Slide 3 text

UI blocking BLOCKS THE MAIN THREAD

Slide 4

Slide 4 text

Asynchronous way

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

CALLBACK HELL

Slide 7

Slide 7 text

Callback Hell A callback is a function that will be executed after another function has finished executing. And if there are a series of things to be done synchronously then we will fall into callback hell that can lead us to ambiguity in understanding the code. It depends on the number of steps and logic we have in our applications

Slide 8

Slide 8 text

RxJava/RxKotlin We can also opt for RxX stack from Reactive world, but that mean we are required to add another tool in our knowledge domain as well as our project.

Slide 9

Slide 9 text

Some issues with Rx : 1. Learning curve 2. Project level structural changes 3. Dependency on 3rd Party.

Slide 10

Slide 10 text

Combine the best of 2 worlds Async + Sync = Coroutines

Slide 11

Slide 11 text

Why I love Coroutines? ❤ Asynchronicity expressed as sequential code that is easy to read and reason about!

Slide 12

Slide 12 text

What are Coroutines? Coroutines are nothing but light-weight threads. Coroutines provide us an easy way to do synchronous and asynchronous programming.

Slide 13

Slide 13 text

From Official docs, “One can think of a coroutine as a light-weight thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.”

Slide 14

Slide 14 text

Sample code for coroutine

Slide 15

Slide 15 text

Sample code for coroutine Blocking Code

Slide 16

Slide 16 text

Suspend behind the scenes

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Coroutine Basic Terminology 1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope

Slide 20

Slide 20 text

1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope Determines what thread or threads the corresponding coroutine uses for its execution. The coroutine dispatcher can confine coroutine execution to a specific thread, dispatch it to a thread pool, or let it run unconfined.

Slide 21

Slide 21 text

1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope ● Dispatchers.IO ● Dispatchers.Main ● Dispatchers.Default

Slide 22

Slide 22 text

1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library.

Slide 23

Slide 23 text

1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope Coroutine Task

Slide 24

Slide 24 text

1. Dispatchers 2. CoroutineContext 3. Job 4. CoroutineScope The scope in which the job will run.

Slide 25

Slide 25 text

Coroutines in work

Slide 26

Slide 26 text

Coroutine Scope

Slide 27

Slide 27 text

Coroutine Scope In android, we have : ● viewModelScope ● lifeCycleScope

Slide 28

Slide 28 text

Coroutine Scope In android, we have : ● viewModelScope ● lifeCycleScope

Slide 29

Slide 29 text

Coroutine Scope In android, we have : ● viewModelScope ● lifeCycleScope

Slide 30

Slide 30 text

Coroutine Scope

Slide 31

Slide 31 text

Coroutine Context A coroutine context is an indexed set of elements that define the behaviour of a Coroutine.

Slide 32

Slide 32 text

Coroutine Context ● CoroutineDispatcher ○ Dispatchers.IO ○ Dispatchers.Default ○ Dispatchers.Main ● CoroutineExceptionHandler ● CoroutineName ● Job

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Some Use cases of Coroutine: 1. Parallel/Series Network Call 2. Handling Exceptions 3. Managing Thread for Disk operations

Slide 36

Slide 36 text

Practical Time

Slide 37

Slide 37 text

Difference between launch and async The difference is that launch returns a Job and does not carry any resulting value, while async returns a Deferred -- a light-weight non-blocking future that represents a promise to provide a result later. So async starts a background thread, does something, and returns a token immediately as Deferred .

Slide 38

Slide 38 text

Coroutine Advanced Jobs : Supervisor Jobs, Child Jobs. Continuation Cancelling Job

Slide 39

Slide 39 text

QnA Time Any questions ?? Feel free to drop any queries and follow me at @i_am_rooparsh