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
0
110
Kotlin Flow
An early look at Kotlin Flow
José Caique Oliveira
June 21, 2019
Tweet
Share
More Decks by José Caique Oliveira
See All by José Caique Oliveira
Coroutines And Flow
jcaiqueoliveira
2
100
Testing your app
jcaiqueoliveira
0
260
Modularizando seu app
jcaiqueoliveira
0
68
Nova Api de Localização Android
jcaiqueoliveira
0
72
Arquitetura para android
jcaiqueoliveira
6
320
Kotlin por onde começar?
jcaiqueoliveira
1
88
Introdução ao Android
jcaiqueoliveira
1
79
Arquitetura para projetos Android
jcaiqueoliveira
1
210
Kotlin 1.1
jcaiqueoliveira
0
120
Other Decks in Programming
See All in Programming
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
820
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
140
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
160
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
360
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
230
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
500
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
360
Beyond ORM
77web
11
1.5k
Zoneless Testing
rainerhahnekamp
0
150
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
360
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
410
バグを見つけた?それAppleに直してもらおう!
uetyo
0
210
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Automating Front-end Workflow
addyosmani
1366
200k
Visualization
eitanlees
146
15k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Practical Orchestrator
shlominoach
186
10k
Mobile First: as difficult as doing things right
swwweet
222
9k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Producing Creativity
orderedlist
PRO
343
39k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
190
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Writing Fast Ruby
sferik
628
61k
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