0% found this document useful (0 votes)
283 views

Kotlin Flow API - Android Cheat Sheet 1.1 - Feb 2022

The document provides a summary of Kotlin's Flow API and its types for asynchronous data streams. It lists the main Flow types - Flow, SharedFlow, MutableSharedFlow, StateFlow, and MutableStateFlow. For each it describes their supertype, features, implementation details, and common Android use cases. callbackFlow is also summarized as a way to emit values from different coroutine contexts into a Flow.

Uploaded by

Umer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
283 views

Kotlin Flow API - Android Cheat Sheet 1.1 - Feb 2022

The document provides a summary of Kotlin's Flow API and its types for asynchronous data streams. It lists the main Flow types - Flow, SharedFlow, MutableSharedFlow, StateFlow, and MutableStateFlow. For each it describes their supertype, features, implementation details, and common Android use cases. callbackFlow is also summarized as a way to emit values from different coroutine contexts into a Flow.

Uploaded by

Umer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Kotlin Flow API ~ Android cheat sheet

RxJava
Type Supertype Description Features Implementation Code example Android usecase
equivalent

• Flow starts separately for


An asynchronous data • General go to type General data type for multi
each collector
stream that sequentially • Can be converted to shot asynchronous data
Flow - • Various intermediate & ter- Example A Flowable
emits values and completes SharedFlow / StateFlow streams like the many call-
minal operators
normally or with an excep- with .shareIn and .stateIn Example B backs inside the View.java
• Automatic backpressure (Observable with BPM)
tion (Unicast broadcaster). operators. class or remote server data.
management

• All subscribes receive all Useful for broadcasting ex-


A Flow shared between mul- • Can be configured with
emited values pensive events to subscrib-
tiple collectors (aka sub- sharing strategy from the Example A
SharedFlow Flow • n most recent values are PublishSubject ers that can come and go.
scribers), so that only one SharingStarted interface.
saved in replay cache Example B Like sharing remote gps
flow is effectively run (Multi- • Eagerly, Lazily and While-
• New subscribers get the (Starts with no value) data between multipe activi-
cast broadcaster). Subscribed
replay cache & new values ties

• emit() function to update


A mutable SharedFlow that value
MutableSharedFlow SharedFlow & provides functions to emit Same as SharedFlow See SharedFlow
See SharedFlow PublishSubject Same as SharedFlow
• tryEmit() function for
values to the flow. non-suspending updates
FlowCollector (Starts with no value)

A specialized and limited • Always has an initial value • Can be configured with Similar to LiveData but with
version of SharedFlow that • Fixed replaysize of 1 sharing strategy from the far more operators and not
StateFlow • No buffering SharingStarted interface. Example A limited to mainthread. Rec-
SharedFlow requires an initial value and BehaviorSubject
emits a read only single • Read access to current • Eagerly, Lazily and While- ommended for KMM proj-
Example B
data value to its subscribers. value without collecting Subscribed (Always emits something) ects. Does require manual
lifecycle management.

• Read/write access to cur-


rent value without collecting MutablaLiveData equivalent,
MutableStateFlow StateFlow & A mutable StateFlow that • Setting the same value as Same See
SeeStateFlow BehaviorSubject
Sameas
asStateFlow
StateFlow StateFlow same up- and downsides
provides a setter for value. before does nothing (dis- from StateFlow apply
MutableSharedFlow tinct until changed built in) (Always emits something)

Creates a Flow and allows • Conceptually very similar • Useful when you need to Converting multi shot An-
values to be emitted from a to a blocking queue run computations
Cold (emitsinlazy)
differ- Example A droid (Java) callback API’s
callbackFlow - different CoroutineContext. • Default capacity of 64 ele- - to a Flow like the onLoca-
ent CoroutineContexts Example B
Uses a hot SendChannel ments • Can convert cluncky call- tionResult() and the onTex-
(function, returns Flow) internally. • Buffer can be configured tChanged() listeners.
back API’s to nicer Flows

Version 1.1 ~ March 2021


by Remy Benza

You might also like