Slide 1

Slide 1 text

Coroutine Retrofit Android Developer Day

Slide 2

Slide 2 text

Raka Adi Nugroho Software Engineer @Tokopedia Kadang nongkrong di www.blogtoandroid.com github.com/rakaadinugroho medium.com/rakaadinugroho

Slide 3

Slide 3 text

Say Hello to Coroutine What is that Why Coroutine? What can Coroutine Do? Agenda Coroutine met Retrofit

Slide 4

Slide 4 text

Coroutine Basic Usage Feel Code with Coroutine Goals Explore more about Coroutine

Slide 5

Slide 5 text

Lagging?

Slide 6

Slide 6 text

Lagging? oh no!

Slide 7

Slide 7 text

Asynchrony refers to the occurrence of events independent of the main program flow and ways to deal with such events - wikipedia

Slide 8

Slide 8 text

On Android

Slide 9

Slide 9 text

On Android

Slide 10

Slide 10 text

Google developers highly recommend using Kotlin Coroutines for doing asynchronous work and when integrating Jetpack.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Coroutine? A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously.

Slide 13

Slide 13 text

Coroutine? The Kotlin team defines coroutines as “lightweight threads” Coroutines similar to Threads Coroutine #1 Coroutine #N Thread

Slide 14

Slide 14 text

Concurrency?Parallel?

Slide 15

Slide 15 text

Concurrency? Parallel?

Slide 16

Slide 16 text

Concurrency? Parallel? Concurrency is Structure | Parallel is execution

Slide 17

Slide 17 text

Suspending Coroutine #1 Thread Backbone of Coroutine

Slide 18

Slide 18 text

Suspending

Slide 19

Slide 19 text

Job Cancellable has Lifecycle Destroy(s) when Complete Deffered Implement Job await() to get Result Coroutine Job

Slide 20

Slide 20 text

launch fire forget return as Job async fire giving result return as Deffered runBlocking Blocks current Thread Coroutine Builder

Slide 21

Slide 21 text

1 2 3

Slide 22

Slide 22 text

1 2 3

Slide 23

Slide 23 text

1 2 3

Slide 24

Slide 24 text

Solve 2 Primary Problem Manage long-running tasks that might otherwise block the main thread and cause your app to freeze. Providing main-safety, or safely calling network or disk operations from the main thread.

Slide 25

Slide 25 text

Solve 2 Primary Problem Manage long-running tasks that might otherwise block the main thread and cause your app to freeze. Providing main-safety, or safely calling network or disk operations from the main thread.

Slide 26

Slide 26 text

Http Communication

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Welcome! Retrofit 2.6.0

Slide 29

Slide 29 text

Before 2.6.0

Slide 30

Slide 30 text

After 2.6.0

Slide 31

Slide 31 text

After 2.6.0 1 2 3

Slide 32

Slide 32 text

Jetpack

Slide 33

Slide 33 text

1 2 Jetpack Suspend Search Journal Coroutine # Search Resume Search Journal

Slide 34

Slide 34 text

Jetpack

Slide 35

Slide 35 text

1 2 Jetpack

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Hmmm... Suspending Function Just Return Single Result!

Slide 38

Slide 38 text

Coroutines Flow

Slide 39

Slide 39 text

Coroutines Flow

Slide 40

Slide 40 text

Coroutines Flow 1 2 3

Slide 41

Slide 41 text

Coroutines Flow

Slide 42

Slide 42 text

Coroutines Flow

Slide 43

Slide 43 text

How to transfer a stream of values?

Slide 44

Slide 44 text

Coroutine Channel(s)

Slide 45

Slide 45 text

Channels provide a way to transfer a stream of values.

Slide 46

Slide 46 text

Channel

Slide 47

Slide 47 text

illustration - Problem

Slide 48

Slide 48 text

illustrations - Solution

Slide 49

Slide 49 text

Sample - 1 1 2 3

Slide 50

Slide 50 text

Sample - 2 1 2

Slide 51

Slide 51 text

Sample - 2 1 2

Slide 52

Slide 52 text

Sample - 2 1 2

Slide 53

Slide 53 text

Wrap Up > Coroutines make asynchronous easily > Support to Several Android JetPack > Kotlin! yeah > Coroutine could be Alternative Solution

Slide 54

Slide 54 text

Reference https://medium.com/ua-makers/impressions-of-google-i-o-2019-kotlin-coroutines-19b6af1e0e3 https://developer.android.com/kotlin/coroutines https://proandroiddev.com/kotlin-coroutines-channels-csp-android-db441400965f
 https://medium.com/@elye.project/understanding-suspend-function-of-coroutines-de26b070c5ed Coroutine 101 - Kotlin Everywhere

Slide 55

Slide 55 text

No content