Slide 1

Slide 1 text

Coroutines and Flow with Architecture Components Paolo Rotolo @paolorotolo

Slide 2

Slide 2 text

Long running tasks ● Network requests ● Database operations ● Image processing ● ...

Slide 3

Slide 3 text

Execute on a non-UI thread

Slide 4

Slide 4 text

Execute on a non-UI thread Get informed when the task is done

Slide 5

Slide 5 text

Execute on a non-UI thread Get informed when the task is done Android solutions ● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with callbacks

Slide 6

Slide 6 text

Execute on a non-UI thread Get informed when the task is done Android solutions ● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with callbacks

Slide 7

Slide 7 text

Execute on a non-UI thread Get informed when the task is done Android solutions ● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with callbacks

Slide 8

Slide 8 text

Execute on a non-UI thread Get informed when the task is done callbacks!!! ● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with Android solutions

Slide 9

Slide 9 text

● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions

Slide 10

Slide 10 text

● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month!

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

13

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

17

Slide 18

Slide 18 text

● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month!

Slide 19

Slide 19 text

● AsyncTask ● RxJava ● Thread / HandlerThread / Executor with Execute on a non-UI thread Get informed when the task is done callbacks!!!1 Android solutions Deprecated this month! Need to handle cancellation

Slide 20

Slide 20 text

Kotlin Coroutines

Slide 21

Slide 21 text

Kotlin Coroutines simple state machine

Slide 22

Slide 22 text

Threads and callbacks fun login(name, pass) { val thread = Thread(Runnable() -> { requestLogin(name, pass) { result -> show(result) } } thread.start() }

Slide 23

Slide 23 text

Threads and callbacks fun login(name, pass) { val thread = Thread(Runnable() -> { requestLogin(name, pass) { result -> Handler(Looper.getMainLooper()) { show(result) } } }

Slide 24

Slide 24 text

Threads and callbacks fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } }

Slide 25

Slide 25 text

onDraw onDraw onDraw requestLogin UI Thread fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } }

Slide 26

Slide 26 text

onDraw onDraw onDraw requestLogin UI Thread fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } }

Slide 27

Slide 27 text

onDraw onDraw onDraw requestLogin UI Thread show onDraw fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } }

Slide 28

Slide 28 text

onDraw onDraw onDraw requestLogin UI Thread show onDraw fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } } onDraw onDraw onDraw

Slide 29

Slide 29 text

Coroutine = suspendable computation onDraw onDraw onDraw requestLogin UI Thread show onDraw onDraw onDraw onDraw

Slide 30

Slide 30 text

When all of the coroutines on the main thread are suspended, the main thread is free to do other work.

Slide 31

Slide 31 text

Kotlin Coroutines simple state machine

Slide 32

Slide 32 text

Kotlin Coroutines simple state machine suspend keyword

Slide 33

Slide 33 text

Kotlin Coroutines simple state machine suspend keyword dispatchers

Slide 34

Slide 34 text

Dispatchers.Main ● Main thread ● Use it for: UI operations

Slide 35

Slide 35 text

Dispatchers.Default ● Common pool of shared background threads ● Use it for: computing-intensive coroutines Dispatchers.Main ● Main thread ● Use it for: UI operations

Slide 36

Slide 36 text

Dispatchers.Default ● Common pool of shared background threads ● Use it for: computing-intensive coroutines Dispatchers.IO ● Shared pool of on-demand created threads ● Use it for: IO-intensive blocking operations Dispatchers.Main ● Main thread ● Use it for: UI operations

Slide 37

Slide 37 text

Dispatchers.Default ● Common pool of shared background threads ● Use it for: computing-intensive coroutines Dispatchers.IO ● Shared pool of on-demand created threads ● Use it for: IO-intensive blocking operations Dispatchers.Main ● Main thread ● Use it for: UI operations Common thread pools!

Slide 38

Slide 38 text

Dispatchers.Default ● Common pool of shared background threads ● Use it for: computing-intensive coroutines Dispatchers.IO ● Shared pool of on-demand created threads ● Use it for: IO-intensive blocking operations Dispatchers.Main ● Main thread ● Use it for: UI operations Common thread pools! Easy to switch dispatchers!

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Used for example by libraries

Slide 41

Slide 41 text

Kotlin Coroutines simple state machine suspend keyword dispatchers

Slide 42

Slide 42 text

Kotlin Coroutines simple state machine suspend keyword dispatchers structured concurrency

Slide 43

Slide 43 text

Kotlin Coroutines simple state machine suspend keyword dispatchers structured concurrency (scope)

Slide 44

Slide 44 text

Kotlin Coroutines Hello World

Slide 45

Slide 45 text

Kotlin Coroutines World Hello

Slide 46

Slide 46 text

// prints Hello, World!

Slide 47

Slide 47 text

Kotlin Coroutines simple state machine suspend keyword dispatchers structured concurrency

Slide 48

Slide 48 text

Kotlin Coroutines simple state machine suspend keyword dispatchers structured concurrency <3 Andorid

Slide 49

Slide 49 text

Launching and cancelling coroutines Login button pressed Launch coroutine requestLogin Show login status

Slide 50

Slide 50 text

fun login(name, pass) { requestLogin(name, pass) { result -> show(result) } } requestLogin Thread show Suspension point

Slide 51

Slide 51 text

suspend fun requestLogin( name: String, pass: String ): Result {...}

Slide 52

Slide 52 text

Launching and cancelling coroutines Login button pressed Launch coroutine requestLogin Show login status

Slide 53

Slide 53 text

Login button pressed Launch coroutine requestLogin Show login status val job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job)

Slide 54

Slide 54 text

Login button pressed Launch coroutine requestLogin Show login status val job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job) fun login(username: String, pass: String ) = uiScope.launch { val result = requestLogin(name, pass) show(result) }

Slide 55

Slide 55 text

Login button pressed Launch coroutine requestLogin Show login status val job = Job() val uiScope = CoroutineScope(Dispatchers.Main + job) fun login(username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) }

Slide 56

Slide 56 text

Launching and cancelling coroutines Login button pressed Launch coroutine requestLogin Show login status Activity destroyed

Slide 57

Slide 57 text

Launching and cancelling coroutines Login button pressed Launch coroutine requestLogin Show login status Activity destroyed Coroutine leak

Slide 58

Slide 58 text

Coroutines + ViewModel

Slide 59

Slide 59 text

Launching and cancelling coroutines Login button pressed Launch coroutine requestLogin Show login status ViewModel

Slide 60

Slide 60 text

60

Slide 61

Slide 61 text

Login button pressed Launch coroutine requestLogin Show login status ViewModel fun onCleared() { super.onCleared() job.cancel() }

Slide 62

Slide 62 text

class MyViewModel: ViewModel() { val viewModelJob = Job() val uiScope = CoroutineScope(Dispatchers.Main+viewModelJob) fun login( username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } fun onCleared() { super.onCleared() viewModelJob.cancel() } }

Slide 63

Slide 63 text

class MyViewModel: ViewModel() { val viewModelJob = Job() val uiScope = CoroutineScope(Dispatchers.Main+viewModelJob) fun login( username: String, pass: String ) = uiScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } fun onCleared() { super.onCleared() viewModelJob.cancel() } } lifecycle-viewmodel-ktx

Slide 64

Slide 64 text

class MyViewModel: ViewModel() { fun login( username: String, pass: String ) = viewModelScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } }

Slide 65

Slide 65 text

suspend does not mean background

Slide 66

Slide 66 text

class MyViewModel: ViewModel() { fun login( username: String, pass: String ) = viewModelScope.launch(Dispatchers.IO) { val result = requestLogin(name, pass) show(result) } }

Slide 67

Slide 67 text

Coroutines + Lifecycle

Slide 68

Slide 68 text

Coroutines + Lifecycle viewmodel.viewModelScope

Slide 69

Slide 69 text

Coroutines + Lifecycle activity.lifecycleScope viewmodel.viewModelScope

Slide 70

Slide 70 text

Coroutines + Lifecycle activity.lifecycleScope fragment.lifecycleScope fragment.viewLifecycleOwner viewmodel.viewModelScope

Slide 71

Slide 71 text

Coroutines + Lifecycle Examples

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Coroutines + Lifecycle

Slide 74

Slide 74 text

Coroutines + Lifecycle React to Lifecycle.State

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Coroutines + LiveData

Slide 78

Slide 78 text

Coroutines + LiveData getUser Example

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

Coroutines + LiveData LiveData Builder

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

Coroutines + LiveData Builder Examples

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Coroutines + LiveData getUser Example

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Coroutines + LiveData LiveData Builder + other LiveData!

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Coroutines + Room

Slide 101

Slide 101 text

Coroutines + Room Suspending DAO functions

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Coroutines + Room Behind the scenes

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Runs on IO threads

Slide 106

Slide 106 text

Runs on IO threads IO-Thread-1

Slide 107

Slide 107 text

Runs on IO threads IO-Thread-1 IO-Thread-2

Slide 108

Slide 108 text

Android SQLite transactions are thread confined

Slide 109

Slide 109 text

Runs on IO threads IO-Thread-1 IO-Thread-2

Slide 110

Slide 110 text

Runs on IO threads IO-Thread-1 IO-Thread-2 Will block until transaction in Io-Thread-1 ends

Slide 111

Slide 111 text

Runs on IO threads IO-Thread-1 IO-Thread-2 Will block until transaction in Io-Thread-1 ends DEADLOCK!

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Coroutines + Room Behind the scenes Coroutines executor

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Immediately execute the callable

Slide 118

Slide 118 text

Immediately execute the callable Use Room dispatchers

Slide 119

Slide 119 text

Immediately execute the callable Use Room dispatchers Free main-safety!

Slide 120

Slide 120 text

Coroutines + WorkManager

Slide 121

Slide 121 text

Coroutines + WorkManager Types of background work

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

Coroutines Coroutines

Slide 124

Slide 124 text

Coroutines Coroutines Foregroun dService

Slide 125

Slide 125 text

Coroutines Coroutines Foregroun dService WorkManager

Slide 126

Slide 126 text

Coroutines + WorkManager Simple Example

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

Defaults to Dispatcher.Default

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

Coroutines + WorkManager WorkManager -> ForegroundService

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

suspendCancellableCoroutine Make callbacks Great again

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

suspendCancellableCoroutine Structured Concurrency

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

Coroutines + Flows

Slide 148

Slide 148 text

Coroutines + Flows Cold stream of data

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

Coroutines + Flow Flow builder

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

Coroutines + Flow Example: Firebase Realtime

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

Questions? Paolo Rotolo @paolorotolo

Slide 164

Slide 164 text

Thank you! Paolo Rotolo @paolorotolo