Slide 1

Slide 1 text

Flowing in the Deep Exploring Event Streams in Kotlin

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Hannes Dorfmann sockeqwe Gabriel Ittner gabrielittner

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

W FL

Slide 6

Slide 6 text

Flowing in the Deep • Introduction • Operators • Threading • Useful APIs

Slide 7

Slide 7 text

fun doHttpRequest() : X { // A long running operation ... return X }

Slide 8

Slide 8 text

class MyActivity : Activity() { override fun onCreate(b: Bundle) { doHttpRequest() } }

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

class MyActivity : Activity() { override fun onCreate(b: Bundle) { doHttpRequest() } }

Slide 11

Slide 11 text

class MyActivity : Activity() { override fun onCreate(b: Bundle) { thread { doHttpRequest() } } }

Slide 12

Slide 12 text

class MyActivity : Activity() { override fun onCreate(b: Bundle) { thread { val x = doHttpRequest() ??? } } }

Slide 13

Slide 13 text

fun doHttpRequest() : X { // A long running operation ... return X }

Slide 14

Slide 14 text

fun doHttpRequest( callback: (X) -> Unit ){ // A long running operation ... callback(X) }

Slide 15

Slide 15 text

fun doHttpRequest( callback: (X) -> Unit ){ thread { // A long running operation ... callback(X) } }

Slide 16

Slide 16 text

class MyActivity : Activity() { override fun onCreate(b: Bundle) { doHttpRequest { result -> // do something with the result } } }

Slide 17

Slide 17 text

doHttpRequest { ... }

Slide 18

Slide 18 text

doHttpRequest { doHttpRequest { ... } }

Slide 19

Slide 19 text

doHttpRequest { doHttpRequest { doHttpRequest { ... } } }

Slide 20

Slide 20 text

doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { ... } } } }

Slide 21

Slide 21 text

doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { ... } } } } }

Slide 22

Slide 22 text

doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { ... } } } } } }

Slide 23

Slide 23 text

doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { doHttpRequest { ... } } } } } } }

Slide 24

Slide 24 text

Callback Hell

Slide 25

Slide 25 text

fun doHttpRequest( callback: (X) -> Unit ) { // A long running operation ... callback(X) }

Slide 26

Slide 26 text

suspend fun doHttpRequest(): X { // A long running operation ... return X }

Slide 27

Slide 27 text

launch { val a = doHttpRequest() val b = doHttpRequest() val c = doHttpRequest() val d = doHttpRequest() val e = doHttpRequest() val f = doHttpRequest() val g = doHttpRequest() }

Slide 28

Slide 28 text

suspend fun doHttpRequest(): X { // A long running operation ... return X }

Slide 29

Slide 29 text

fun doHttpRequest(continuation : Continuation) { // A long running operation ... }

Slide 30

Slide 30 text

fun doHttpRequest(continuation : Continuation) { // A long running operation ... contination.resume(X) }

Slide 31

Slide 31 text

fun doHttpRequest( callback: (X) -> Unit ){ // A long running operation ... callback(X) }

Slide 32

Slide 32 text

fun doHttpRequest( observer: (X) -> Unit ){ // A long running operation ... observer(X) }

Slide 33

Slide 33 text

Observer Pattern

Slide 34

Slide 34 text

fun doHttpRequest( observer: (X) -> Unit ){ // A long running operation ... observer(X) }

Slide 35

Slide 35 text

fun downloadVideo( observer: (progress) -> Unit ) { }

Slide 36

Slide 36 text

fun downloadVideo( observer: (progress) -> Unit ) { // A long running operation ... observer(0.1) ... observer(0.2) ... observer(1.0) }

Slide 37

Slide 37 text

suspend fun downloadVideo() : ??? { // A long running operation ... ??? } With Coroutines?

Slide 38

Slide 38 text

fun downloadVideo( observer: (progress) -> Unit )

Slide 39

Slide 39 text

interface Downloader { fun downloadVideo( observer: (progress) -> Unit ) }

Slide 40

Slide 40 text

interface Downloader { fun download( observer: (progress) -> Unit ) }

Slide 41

Slide 41 text

interface Downloader { fun download( observer: (T) -> Unit ) }

Slide 42

Slide 42 text

interface Downloader { fun download( observer: (T) -> Unit ) } interface DownloadObserver

Slide 43

Slide 43 text

interface Downloader { fun download( observer: (T) -> Unit ) } interface DownloadObserver { fun emit( value: T ) }

Slide 44

Slide 44 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) }

Slide 45

Slide 45 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) } interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) }

Slide 46

Slide 46 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) } interface Downloader { fun collect( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) }

Slide 47

Slide 47 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) } interface Downloader { fun collect( c: Collector ) } interface Collector { fun emit( value: T ) }

Slide 48

Slide 48 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) } interface Flow { fun collect( c: Collector ) } interface Collector { fun emit( value: T ) }

Slide 49

Slide 49 text

interface Downloader { fun download( o: DownloadObserver ) } interface DownloadObserver { fun emit( value: T ) } interface Flow { fun collect( c: FlowCollector ) } interface FlowCollector { fun emit( value: T ) }

Slide 50

Slide 50 text

interface Flow { fun collect( c: FlowCollector ) } interface FlowCollector { fun emit( value: T ) } Flow

Slide 51

Slide 51 text

interface Flow { suspend fun collect( c: FlowCollector ) } interface FlowCollector { suspend fun emit( value: T ) } Flow

Slide 52

Slide 52 text

fun downloadVideo(): ??? { ... ??? }

Slide 53

Slide 53 text

fun downloadVideo(): Flow { ... ??? }

Slide 54

Slide 54 text

fun flow(block: suspend FlowCollector.() -> Unit): Flow fun downloadVideo(): Flow { ... ??? }

Slide 55

Slide 55 text

fun flow(block: suspend FlowCollector.() -> Unit): Flow fun downloadVideo(): Flow = flow { ... ??? }

Slide 56

Slide 56 text

fun flow(block: suspend FlowCollector.() -> Unit): Flow fun downloadVideo(): Flow = flow { // this: FlowCollector ... ??? }

Slide 57

Slide 57 text

fun flow(block: suspend FlowCollector.() -> Unit): Flow fun downloadVideo(): Flow = flow { // this: FlowCollector ... emit(0.1) }

Slide 58

Slide 58 text

fun flow(block: suspend FlowCollector.() -> Unit): Flow fun downloadVideo(): Flow = flow { // this is FlowCollector ... emit(0.1) ... emit(0.2) ... emit(1.0) }

Slide 59

Slide 59 text

Let it Flow!

Slide 60

Slide 60 text

flow { for (i in 0..5) { delay(100) emit(i) } }

Slide 61

Slide 61 text

class MyViewModel : ViewModel() { init { viewModelScope.launch { flow { for (i in 0..5) { delay(100) emit(i) } } } } }

Slide 62

Slide 62 text

class MyViewModel : ViewModel() { init { viewModelScope.launch { flow { for (i in 0..5) { delay(100) emit(i) } } } } } Launch new coroutine

Slide 63

Slide 63 text

class MyViewModel : ViewModel() { init { viewModelScope.launch { flow { for (i in 0..5) { delay(100) emit(i) } } } } } Launch new coroutine 2.1.0-beta01

Slide 64

Slide 64 text

class MyViewModel : ViewModel() { init { viewModelScope.launch { flow { for (i in 0..5) { delay(100) emit(i) } } } } } Suspending block
 
 executed in coroutine

Slide 65

Slide 65 text

launch { flow { for (i in 0..5) { delay(100) emit(i) } } }

Slide 66

Slide 66 text

launch { flow { for (i in 0..5) { delay(100) emit(i) } }.collect { value -> println(value) } }

Slide 67

Slide 67 text

launch { flow { for (i in 0..5) { delay(100) emit(i) } }.collect { value -> println(value) } } Observes flow

Slide 68

Slide 68 text

launch { flow { for (i in 0..5) { delay(100) emit(i) } }.collect { value -> println(value) } } notify observer about value

Slide 69

Slide 69 text

launch { flow { for (i in 0..5) { delay(100) emit(i) } }.collect { value -> println(value) } }

Slide 70

Slide 70 text

What about errors?

Slide 71

Slide 71 text

flow { throw Exception("Something went wrong") }.collect { println(it) }

Slide 72

Slide 72 text

flow { throw Exception("Something went wrong") }.collect { println(it) } re-throws

Slide 73

Slide 73 text

try { flow { throw Exception("Something went wrong") }.collect { println(it) } } catch (e: Exception) { // handle error }

Slide 74

Slide 74 text

flow { emit(0.1) throw Exception("Something went wrong") }.collect { println(it) }

Slide 75

Slide 75 text

flow { emit(0.1) throw Exception("Something went wrong") }.collect { println(it) } sealed class DownloadStatus { }

Slide 76

Slide 76 text

flow { emit(0.1) throw Exception("Something went wrong") }.collect { println(it) } sealed class DownloadStatus { data class Progress(val value: Double) : DownloadStatus() object Success : DownloadStatus() data class Error(val message: String) : DownloadStatus() }

Slide 77

Slide 77 text

flow { emit(DownloadStatus.Progress(0.1)) try { throw IOException(“Something went wrong”)) } catch (e: IOException) { emit(DownloadStatus.Error(e.message)) } }.collect { println(it) } sealed class DownloadStatus { data class Progress(val value: Double) : DownloadStatus() object Success : DownloadStatus() data class Error(val message: String) : DownloadStatus() }

Slide 78

Slide 78 text

And completion?

Slide 79

Slide 79 text

launch { flow { // do stuff }.collect { value -> println(value) } }

Slide 80

Slide 80 text

launch { flow { // do stuff }.collect { value -> println(value) } } Flow gets instantiated

Slide 81

Slide 81 text

launch { flow { // do stuff }.collect { value -> println(value) } } collect gets called

Slide 82

Slide 82 text

launch { flow { // do stuff }.collect { value -> println(value) } } coroutine suspends here

Slide 83

Slide 83 text

launch { flow { // do stuff }.collect { value -> println(value) } } executed collect suspends here

Slide 84

Slide 84 text

launch { flow { // do stuff }.collect { value -> println(value) } } called for emission collect suspends here

Slide 85

Slide 85 text

launch { flow { // do stuff }.collect { value -> println(value) } } executed collect suspends here

Slide 86

Slide 86 text

launch { flow { // do stuff }.collect { value -> println(value) } } called for emission collect suspends here

Slide 87

Slide 87 text

launch { flow { // do stuff }.collect { value -> println(value) } } collect suspends here

Slide 88

Slide 88 text

launch { flow { // do stuff }.collect { value -> println(value) } }

Slide 89

Slide 89 text

launch { flow { // do stuff }.collect { value -> println(value) } handleCompletion() }

Slide 90

Slide 90 text

flow { // do stuff }.onCompletion { handleCompletion() }.collect { value -> println(value) } new in 1.3.0-M 2

Slide 91

Slide 91 text

Operator

Slide 92

Slide 92 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value -> println(value) }

Slide 93

Slide 93 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { }

Slide 94

Slide 94 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { } }

Slide 95

Slide 95 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector } } fun flow(block: suspend FlowCollector.() -> Unit): Flow

Slide 96

Slide 96 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector } }

Slide 97

Slide 97 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> } } }

Slide 98

Slide 98 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> } } } Flow.collect

Slide 99

Slide 99 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> } } }

Slide 100

Slide 100 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value -> println(value) }

Slide 101

Slide 101 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value -> println(value) } fun Flow.map(…)

Slide 102

Slide 102 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value -> println(value) }

Slide 103

Slide 103 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value -> println(value) }

Slide 104

Slide 104 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> } } }

Slide 105

Slide 105 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> } } }

Slide 106

Slide 106 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> val r = transformer(value) } } }

Slide 107

Slide 107 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> val r = transformer(value) emit(r) } } }

Slide 108

Slide 108 text

fun Flow.map(transformer: suspend (value: T) -> R): Flow { return flow { // this: FlowCollector collect { value -> val r = transformer(value) emit(r) } } } FlowCollector.emit(value)

Slide 109

Slide 109 text

flow { for (i in 0..5) { delay(100) emit(i) } }.map { value -> value * 2 }.collect { value println(value) }

Slide 110

Slide 110 text

Operator

Slide 111

Slide 111 text

Operator Observer Pattern

Slide 112

Slide 112 text

flow { for (i in 0..5) { delay(100) emit(i) } }.switchMap { value -> flow { emit(“first $value") delay(500) emit(“second $value") } }.collect { value ->
 println(value) }

Slide 113

Slide 113 text

flow { for (i in 0..5) { delay(100) emit(i) } }.switchMap { value -> flow { emit(“first $value") delay(500) emit(“second $value") } }.collect { value ->
 println(value) } Console output first 0 first 1 first 2 first 3 first 4 first 5

Slide 114

Slide 114 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { }

Slide 115

Slide 115 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { } }

Slide 116

Slide 116 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> } } }

Slide 117

Slide 117 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> } } }

Slide 118

Slide 118 text

flow { for (i in 0..5) { delay(100) emit(i) } }.switchMap { value flow { emit(“first $value") delay(500) emit(“second $value") } }.collect { value ->
 println(value) }

Slide 119

Slide 119 text

flow { for (i in 0..5) { delay(100) emit(i) } }.switchMap { value -> flow { emit(“first $value") delay(500) emit(“second $value") } }.collect { value 
 println(value) }

Slide 120

Slide 120 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> } } }

Slide 121

Slide 121 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> val inner : Flow = mapper(outerValue) } } }

Slide 122

Slide 122 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> val inner : Flow = mapper(outerValue) inner.collect { value -> } } } }

Slide 123

Slide 123 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> val inner : Flow = mapper(outerValue) launch { inner.collect { value -> } } } } }

Slide 124

Slide 124 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> val inner : Flow = mapper(outerValue) launch { inner.collect { value -> emit(value) } } } } }

Slide 125

Slide 125 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { // this: FlowCollector collect { outerValue -> val inner : Flow = mapper(outerValue) launch { inner.collect { value -> emit(value) } } } } } FlowCollector.emit(value)

Slide 126

Slide 126 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { collect { outerValue -> val inner : Flow = mapper(outerValue) launch { inner.collect { value -> emit(value) } } } } }

Slide 127

Slide 127 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { var currentJob: Job? = null collect { outerValue -> val inner : Flow = mapper(outerValue) currentJob = launch { inner.collect { value -> emit(value) } } } } }

Slide 128

Slide 128 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { var currentJob: Job? = null collect { outerValue -> val inner : Flow = mapper(outerValue) currentJob?.cancelAndJoin() currentJob = launch { inner.collect { value -> emit(value) } } } } }

Slide 129

Slide 129 text

fun Flow.switchMap( mapper: suspend (value: T) -> Flow ): Flow { return flow { coroutineScope { var currentJob: Job? = null collect { outerValue -> val inner : Flow = mapper(outerValue) currentJob?.cancelAndJoin() currentJob = launch { inner.collect { value -> emit(value) } } } } } }

Slide 130

Slide 130 text

Threading

Slide 131

Slide 131 text

interface CoroutineContext

Slide 132

Slide 132 text

interface CoroutineContext interface Job : CoroutineContext { public fun cancel(): Unit // a lot more… }

Slide 133

Slide 133 text

interface CoroutineContext interface Job : CoroutineContext { public fun cancel(): Unit // a lot more… } abstract class CoroutineDispatcher

Slide 134

Slide 134 text

interface CoroutineContext interface Job : CoroutineContext { public fun cancel(): Unit // a lot more… } abstract class CoroutineDispatcher Dispatchers.Main Dispatchers.Default Dispatchers.IO Dispatchers.Unconfined newSingleThreadContext()

Slide 135

Slide 135 text

interface CoroutineContext interface Job : CoroutineContext { public fun cancel(): Unit // a lot more… } abstract class CoroutineDispatcher Dispatchers.Main Dispatchers.Default Dispatchers.IO Dispatchers.Unconfined newSingleThreadContext() val context: CoroutineContext = Job() + Dispatchers.IO

Slide 136

Slide 136 text

public interface CoroutineScope { public val coroutineContext: CoroutineContext }

Slide 137

Slide 137 text

public interface CoroutineScope { public val coroutineContext: CoroutineContext } public inline fun CoroutineScope.cancel()

Slide 138

Slide 138 text

public interface CoroutineScope { public val coroutineContext: CoroutineContext } public inline fun CoroutineScope.cancel() public fun CoroutineScope.launch(...)

Slide 139

Slide 139 text

public interface CoroutineScope { public val coroutineContext: CoroutineContext } public inline fun CoroutineScope.cancel() public fun CoroutineScope.launch(...) val ViewModel.viewModelScope: CoroutineScope val Lifecycle.coroutineScope: LifecycleCoroutineScope

Slide 140

Slide 140 text

Threading (actually)

Slide 141

Slide 141 text

lifecycle.coroutineScope.launchWhenStarted { }

Slide 142

Slide 142 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 143

Slide 143 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread

Slide 144

Slide 144 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread

Slide 145

Slide 145 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread

Slide 146

Slide 146 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread

Slide 147

Slide 147 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 148

Slide 148 text

lifecycle.coroutineScope.launchWhenStarted { flow { GlobalScope.launch { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 149

Slide 149 text

lifecycle.coroutineScope.launchWhenStarted { flow { withContext(Dispatchers.IO) { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 150

Slide 150 text

lifecycle.coroutineScope.launchWhenStarted { flow { withContext(Dispatchers.IO) { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } } IllegalStateException

Slide 151

Slide 151 text

lifecycle.coroutineScope.launchWhenStarted { flow { withContext(Dispatchers.IO) { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 152

Slide 152 text

lifecycle.coroutineScope.launchWhenStarted { channelFlow { withContext(Dispatchers.IO) { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 153

Slide 153 text

lifecycle.coroutineScope.launchWhenStarted { channelFlow { // this: ProducerScope withContext(Dispatchers.IO) { // expensive operation emit(progress) // expensive operation emit(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 154

Slide 154 text

lifecycle.coroutineScope.launchWhenStarted { channelFlow { // this: ProducerScope withContext(Dispatchers.IO) { // expensive operation send(progress) // expensive operation send(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 155

Slide 155 text

lifecycle.coroutineScope.launchWhenStarted { channelFlow { // this: ProducerScope withContext(Dispatchers.IO) { // expensive operation send(progress) // expensive operation send(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread

Slide 156

Slide 156 text

lifecycle.coroutineScope.launchWhenStarted { channelFlow { // this: ProducerScope withContext(Dispatchers.IO) { // expensive operation send(progress) // expensive operation send(result) } }.map { it + 1 }.map { it.toString() }.collect { println(it) } } main thread IO thread

Slide 157

Slide 157 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 }.map { it.toString() }.collect { println(it) } }

Slide 158

Slide 158 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 } .flowOn(Dispatchers.IO) .map { it.toString() }.collect { println(it) } }

Slide 159

Slide 159 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 } .flowOn(Dispatchers.IO) .map { it.toString() }.collect { println(it) } } main thread IO thread

Slide 160

Slide 160 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 } .flowOn(Dispatchers.IO) .map { it.toString() }.collect { println(it) } } main thread IO thread

Slide 161

Slide 161 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 } .flowOn(Dispatchers.IO) .map { it.toString() }.collect { println(it) } } main thread IO thread

Slide 162

Slide 162 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation }.map { it + 1 } .flowOn(Dispatchers.IO) .map { it.toString() }.collect { println(it) } }

Slide 163

Slide 163 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation } .flowOn(Dispatchers.IO) .map { it + 1 } .flowOn(Dispatchers.Default) .map { it.toString() }.collect { println(it) } }

Slide 164

Slide 164 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation } .flowOn(Dispatchers.IO) .map { it + 1 } .flowOn(Dispatchers.Default) .map { it.toString() }.collect { println(it) } } main thread IO thread Default thread

Slide 165

Slide 165 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation } .flowOn(Dispatchers.IO) .map { it + 1 } .flowOn(Dispatchers.Default) .map { it.toString() }.collect { println(it) } } main thread IO thread Default thread

Slide 166

Slide 166 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation } .flowOn(Dispatchers.IO) .map { it + 1 } .flowOn(Dispatchers.Default) .map { it.toString() }.collect { println(it) } } main thread IO thread Default thread

Slide 167

Slide 167 text

lifecycle.coroutineScope.launchWhenStarted { flow { // expensive operation } .flowOn(Dispatchers.IO) .map { it + 1 } .flowOn(Dispatchers.Default) .map { it.toString() }.collect { println(it) } } main thread IO thread Default thread

Slide 168

Slide 168 text

Threading

Slide 169

Slide 169 text

Threading • collect lambda is always called on the initial CoroutineContext

Slide 170

Slide 170 text

Threading • collect lambda is always called on the initial CoroutineContext • flowOn does only influence the upstream

Slide 171

Slide 171 text

Threading • collect lambda is always called on the initial CoroutineContext • flowOn does only influence the upstream • nothing can change the context of downstream emissions

Slide 172

Slide 172 text

Some useful APIs

Slide 173

Slide 173 text

flowOf(1, 2, 3, 4)

Slide 174

Slide 174 text

flowOf(1, 2, 3, 4) listOf(1, 2, 3, 4).asFlow()

Slide 175

Slide 175 text

flowOf(1, 2, 3, 4) listOf(1, 2, 3, 4).asFlow() sequenceOf(1, 2, 3, 4).asFlow()

Slide 176

Slide 176 text

val someFunction: (() -> Result) = ...

Slide 177

Slide 177 text

val someFunction: (() -> Result) = ... someFunction.asFlow()

Slide 178

Slide 178 text

val someFunction: (() -> Result) = ... someFunction.asFlow() val doHttpRequest: (suspend () -> Result) = ...

Slide 179

Slide 179 text

val someFunction: (() -> Result) = ... someFunction.asFlow() val doHttpRequest: (suspend () -> Result) = ... doHttpRequest.asFlow()

Slide 180

Slide 180 text

val observable: Observable = Observable.just(1)

Slide 181

Slide 181 text

val observable: Observable = Observable.just(1) val flow: Flow = observable.toFlowable().asFlow()

Slide 182

Slide 182 text

val observable: Observable = Observable.just(1) val flow: Flow = observable.toFlowable().asFlow() val observable2: Observable = flow.asPublisher().toObservable()

Slide 183

Slide 183 text

RxJava Subject / Relay flatMap() concatMap() switchMap() Flow channel.asFlow() flatMapMerge() flatMapConcat() switchMap()

Slide 184

Slide 184 text

RxJava & Flow map() filter() combineLatest() take() scan() retry() debounce() distinctUntilChanged() zip() takeUntil() reduce()

Slide 185

Slide 185 text

interface MyService { @GET(“{user}/books/favorite”) suspend fun getFavoriteBook(@Path(“user") userId: Int): Book }

Slide 186

Slide 186 text

new in 2.6.0 interface MyService { @GET(“{user}/books/favorite”) suspend fun getFavoriteBook(@Path(“user") userId: Int): Book }

Slide 187

Slide 187 text

flow { emit(userId1) emit(userId2) } interface MyService { @GET("{user}/books/favorite") suspend fun getFavoriteBook(@Path("user") userId: Int): Book }

Slide 188

Slide 188 text

flow { emit(userId1) emit(userId2) }.someOperator { userId -> val book = myService.getFavoriteBook(userId) book } interface MyService { @GET("{user}/books/favorite") suspend fun getFavoriteBook(@Path("user") userId: Int): Book }

Slide 189

Slide 189 text

flow { emit(userId1) emit(userId2) }.someOperator { userId -> val book = myService.getFavoriteBook(userId) book }.collect { book -> println(book) } interface MyService { @GET("{user}/books/favorite") suspend fun getFavoriteBook(@Path("user") userId: Int): Book }

Slide 190

Slide 190 text

flow { emit(userId1) emit(userId2) }.map { userId -> val book = myService.getFavoriteBook(userId) book }.collect { book -> println(book) } interface MyService { @GET("{user}/books/favorite") suspend fun getFavoriteBook(@Path("user") userId: Int): Book }

Slide 191

Slide 191 text

Summary • Flow: event stream • Observer Pattern • Backed by Coroutines

Slide 192

Slide 192 text

sockeqwe gabrielittner Questions?