File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/playground/flow/concurrency Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2
+
3
+ import kotlinx.coroutines.coroutineScope
4
+ import kotlinx.coroutines.delay
5
+ import kotlinx.coroutines.flow.collectLatest
6
+ import kotlinx.coroutines.flow.flow
7
+
8
+ suspend fun main () = coroutineScope {
9
+
10
+ val flow = flow {
11
+ repeat(5 ) {
12
+ val pancakeIndex = it + 1
13
+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
14
+ delay(100 )
15
+ println (" Emitter: Pancake $pancakeIndex ready!" )
16
+ emit(pancakeIndex)
17
+ }
18
+ }
19
+
20
+ flow.collectLatest {
21
+ println (" Collector: Start eating pancake $it " )
22
+ delay(300 )
23
+ println (" Collector: Finished eating pancake $it " )
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2
+
3
+ import kotlinx.coroutines.coroutineScope
4
+ import kotlinx.coroutines.delay
5
+ import kotlinx.coroutines.flow.flow
6
+ import kotlinx.coroutines.flow.mapLatest
7
+
8
+ suspend fun main () = coroutineScope {
9
+
10
+ val flow = flow {
11
+ repeat(5 ) {
12
+ val pancakeIndex = it + 1
13
+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
14
+ delay(100 )
15
+ println (" Emitter: Pancake $pancakeIndex ready!" )
16
+ emit(pancakeIndex)
17
+ }
18
+ }.mapLatest {
19
+ println (" Add topping onto the pancake $it " )
20
+ delay(200 )
21
+ it
22
+ }
23
+
24
+ flow.collect {
25
+ println (" Collector: Start eating pancake $it " )
26
+ delay(300 )
27
+ println (" Collector: Finished eating pancake $it " )
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments