Slide 1

Slide 1 text

KOTLIN FLOW JOSÉ CAIQUE

Slide 2

Slide 2 text

produce(capacity = 20) { (0..10).forEach { send(it) println("sent $it") } } //emmit : 1,2,3 … val intFlow = flow { for (i in 0 until 10) { emit(i) println("sent flow $i") } } // intFlow.collect{ }
 // 1,2,3… HOT CHANNEL COLD STREAM implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M1'

Slide 3

Slide 3 text

@ExperimentalCoroutinesApi public interface Flow { @InternalCoroutinesApi public suspend fun collect(collector: FlowCollector) } BackPressure by Default

Slide 4

Slide 4 text

BUILDERS ▸ (0..2).asFlow() ▸ flow{ emit(1) } ▸ flowOf(1, 2, 3, 4, 5) ▸ channelFlow { send(1) } ▸ emptyFlow()

Slide 5

Slide 5 text

(0..1).asFlow() .map { println("Thread0 ${Thread.currentThread().name}") it*it } .filter { println("Thread1 ${Thread.currentThread().name}") it % 4 == 0 } .flowOn(Dispatchers.IO) .map { println("Thread2 ${Thread.currentThread().name}") it * 2 } .flowOn(Dispatchers.Default) Thread0 DefaultDispatcher-worker-2 Thread1 DefaultDispatcher-worker-2 Thread0 DefaultDispatcher-worker-2 Thread1 DefaultDispatcher-worker-2 Thread2 main

Slide 6

Slide 6 text

//requestA(Flow) -> 1000L //requestB(Flow) -> 2000L requestA() .zip(requestB()) { requestA, requestB -> requestA + requestB }.collect { println(it) } Total: ~2100L

Slide 7

Slide 7 text

//requestA(suspend) -> 1000L //requestB(suspend) -> 2000L channelFlow> { val result1 = async { requestA() } val result2 = async { requestB() } val result3 = result1.await() to result2.await() send(result3) }.collect { println(it) } // buffer, still cold stream Total: ~2100L

Slide 8

Slide 8 text

val myFlow = flow { // GlobalScope.launch { // is prohibited // launch(Dispatchers.IO) { // is prohibited // withContext(CoroutineName("myFlow")) // is prohibited emit(1) // OK coroutineScope { emit(2) // OK -- still the same coroutine } }

Slide 9

Slide 9 text

channelFlow { launch(IO){ send(requestA()) } launch(Main) { send(requestB()) } }.collect { println(it) }

Slide 10

Slide 10 text

val broadcast: ConflatedBroadcastChannel // emitting launch { broadcast.send(user) } // listening broadcast.consumeEach { service .retrieve(it) .map { it.getOrNull() } .collect { it?.let { … } } } //close broadcast.close()

Slide 11

Slide 11 text

EXTENSIONS ‣ broadcastIn ‣ buffer ‣ collect ‣ combineLatest ‣ conflate ‣ count ‣ debounce ‣ delayEach ‣ delayFlow ‣ distinctUntilChanged ‣ distinctUntilChangedBy ‣ drop ‣ dropWhile ‣ filter ‣ filterIsInstance ‣ filterNot ‣ filterNotNull ‣ first ‣ flatMapConcat ‣ flatMapMerge ‣ flattenConcat ‣ flattenMerge ‣ flowWith ‣ fold ‣ map ‣ mapNotNull ‣ onEach ‣ onErrorCollect ‣ onErrorReturn ‣ produceIn ‣ reduce ‣ retry ‣ sample ‣ scan ‣ scanFold ‣ scanReduce ‣ single ‣ singleOrNull ‣ switchMap ‣ take ‣ takeWhile ‣ toCollection ‣ toList ‣ toSet ‣ transform ‣ zip

Slide 12

Slide 12 text

fun Flow.startWith(start: T) = flow { collect { value -> emit(start) emit(value) } } flowOf("Caique") .startWith("Jose") .collect { println(it) } 
 // José
 // Caique EASY TO CREATE EXTENSIONS

Slide 13

Slide 13 text

MORE EXTENSIONS ARE COMING https://github.com/Kotlin/kotlinx.coroutines/issues/1263 https://github.com/Kotlin/kotlinx.coroutines/issues/1261 https://github.com/Kotlin/kotlinx.coroutines/issues/1247 https://github.com/Kotlin/kotlinx.coroutines/issues/1186

Slide 14

Slide 14 text

JOSÉ CAIQUE ▸ Software Engineer ▸ Computer Science by UFS ▸ Android Tech Lead at Stone ▸ Speaker

Slide 15

Slide 15 text

J C A I Q U E _ J O S E C A I Q U E J O S E C A I Q U E T H A N K S