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
120
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
110
Testing your app
jcaiqueoliveira
0
290
Modularizando seu app
jcaiqueoliveira
0
72
Nova Api de Localização Android
jcaiqueoliveira
0
85
Arquitetura para android
jcaiqueoliveira
6
320
Kotlin por onde começar?
jcaiqueoliveira
1
92
Introdução ao Android
jcaiqueoliveira
1
83
Arquitetura para projetos Android
jcaiqueoliveira
1
230
Kotlin 1.1
jcaiqueoliveira
0
130
Other Decks in Programming
See All in Programming
Claude CodeによるAI駆動開発の実践 〜そこから見えてきたこれからのプログラミング〜
iriikeita
0
220
そのpreloadは必要?見過ごされたpreloadが技術的負債として爆発した日
mugitti9
2
3.4k
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
10
6.7k
なぜあの開発者はDevRelに伴走し続けるのか / Why Does That Developer Keep Running Alongside DevRel?
nrslib
3
400
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
190
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
490
あなたとKaigi on Rails / Kaigi on Rails + You
shimoju
0
160
Android16 Migration Stories ~Building a Pattern for Android OS upgrades~
reoandroider
0
110
Django Ninja による API 開発効率化とリプレースの実践
kashewnuts
0
1.3k
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
0
270
技術的負債の正体を知って向き合う / Facing Technical Debt
irof
0
170
CSC509 Lecture 05
javiergs
PRO
0
300
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Navigating Team Friction
lara
190
15k
Agile that works and the tools we love
rasmusluckow
331
21k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
The Cost Of JavaScript in 2023
addyosmani
55
9k
Side Projects
sachag
455
43k
The Language of Interfaces
destraynor
162
25k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
How to Ace a Technical Interview
jacobian
280
24k
Designing Experiences People Love
moore
142
24k
The Invisible Side of Design
smashingmag
302
51k
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