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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
José Caique Oliveira
June 21, 2019
Programming
140
0
Share
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
83
Nova Api de Localização Android
jcaiqueoliveira
0
100
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
分析エージェント精度向上における データアナリストの役割
oura_shoya
0
130
TypeScriptだけでAIエージェントを作る フロント・エージェント・インフラのフルスタック実践
har1101
6
1.1k
AIエージェントの隔離技術の徹底比較
kawayu
0
440
AI時代だからこそ「Bloc」を採用する価値があるのかもしれない
takuroabe
0
250
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.1k
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
390
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
630
ビジネスモデルから紐解く、AI+型駆動開発
hirokiomote
2
3.4k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
200
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.1k
Inside Stream API
skrb
1
240
デフォルト運用のCodeRabbit、1年で何が変わったか / How CodeRabbit Changed Our Code Review in 1 Year
bake0937
1
110
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
210
KATA
mclloyd
PRO
35
15k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
470
How STYLIGHT went responsive
nonsquared
100
6.1k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.5k
First, design no harm
axbom
PRO
2
1.2k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Technical Leadership for Architectural Decision Making
baasie
3
380
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.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