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
84
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
88
Testing your app
jcaiqueoliveira
0
240
Modularizando seu app
jcaiqueoliveira
0
57
Nova Api de Localização Android
jcaiqueoliveira
0
58
Arquitetura para android
jcaiqueoliveira
6
310
Kotlin por onde começar?
jcaiqueoliveira
1
79
Introdução ao Android
jcaiqueoliveira
1
66
Arquitetura para projetos Android
jcaiqueoliveira
1
210
Kotlin 1.1
jcaiqueoliveira
0
110
Other Decks in Programming
See All in Programming
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
2
790
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
340
Fragment Composition of GraphQL
quramy
7
1k
雑に思考を整理する技術と効能
konifar
60
29k
Snowflakeで眠ったデータを起こそう!
estie
0
120
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
250
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
940
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
400
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
710
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
210
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
260
12k
Raft: Consensus for Rubyists
vanstee
132
6.3k
The Language of Interfaces
destraynor
151
23k
For a Future-Friendly Web
brad_frost
172
9k
Practical Orchestrator
shlominoach
182
9.7k
The Invisible Customer
myddelton
114
12k
What's new in Ruby 2.0
geeforr
337
31k
The Invisible Side of Design
smashingmag
294
49k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
VelocityConf: Rendering Performance Case Studies
addyosmani
320
23k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
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