Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin Flow
Search
José Caique Oliveira
June 21, 2019
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kotlin Flow
An early look at Kotlin Flow
José Caique Oliveira
June 21, 2019
More Decks by José Caique Oliveira
See All by José Caique Oliveira
Coroutines And Flow
jcaiqueoliveira
2
130
Testing your app
jcaiqueoliveira
0
320
Modularizando seu app
jcaiqueoliveira
0
86
Nova Api de Localização Android
jcaiqueoliveira
0
110
Arquitetura para android
jcaiqueoliveira
6
330
Kotlin por onde começar?
jcaiqueoliveira
1
110
Introdução ao Android
jcaiqueoliveira
1
100
Arquitetura para projetos Android
jcaiqueoliveira
1
250
Kotlin 1.1
jcaiqueoliveira
0
150
Other Decks in Programming
See All in Programming
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
260
New "Type" system on PicoRuby
pocke
1
1k
1B+ /day規模のログを管理する技術
broadleaf
0
110
Contextとはなにか
chiroruxx
1
370
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
140
JavaDoc 再入門
nagise
1
410
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.4k
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
300
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.5k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.5k
Featured
See All Featured
エンジニアに許された特別な時間の終わり
watany
107
250k
How to Ace a Technical Interview
jacobian
281
24k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
620
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
A Soul's Torment
seathinner
6
3k
WCS-LA-2024
lcolladotor
0
650
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
GraphQLとの向き合い方2022年版
quramy
50
15k
GitHub's CSS Performance
jonrohan
1033
470k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
210
RailsConf 2023
tenderlove
30
1.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
Transcript
KOTLIN FLOW JOSÉ CAIQUE
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'
@ExperimentalCoroutinesApi public interface Flow<out T> { @InternalCoroutinesApi public suspend fun
collect(collector: FlowCollector<T>) } BackPressure by Default
BUILDERS ▸ (0..2).asFlow() ▸ flow{ emit(1) } ▸ flowOf(1, 2,
3, 4, 5) ▸ channelFlow { send(1) } ▸ emptyFlow<T>()
(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
//requestA(Flow) -> 1000L //requestB(Flow) -> 2000L requestA() .zip(requestB()) { requestA,
requestB -> requestA + requestB }.collect { println(it) } Total: ~2100L
//requestA(suspend) -> 1000L //requestB(suspend) -> 2000L channelFlow<Pair<Int, String>> { 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
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 } }
channelFlow { launch(IO){ send(requestA()) } launch(Main) { send(requestB()) } }.collect
{ println(it) }
val broadcast: ConflatedBroadcastChannel<User> // emitting launch { broadcast.send(user) } //
listening broadcast.consumeEach { service .retrieve(it) .map { it.getOrNull() } .collect { it?.let { … } } } //close broadcast.close()
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
fun <T> Flow<T>.startWith(start: T) = flow { collect { value
-> emit(start) emit(value) } } flowOf("Caique") .startWith("Jose") .collect { println(it) } // José // Caique EASY TO CREATE EXTENSIONS
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
JOSÉ CAIQUE ▸ Software Engineer ▸ Computer Science by UFS
▸ Android Tech Lead at Stone ▸ Speaker
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