The Difference Between Kotlin Flows
The Difference Between Kotlin Flows
The difference
between Flows
BY VADYM YAROSHCHUK
WHAT ARE KOTLIN FLOWS?
In simple terms, Kotlin Flows are a way to handle sequences of values
asynchronously in Kotlin. It allows you to represent a stream of data
that can be processed sequentially. Unlike Channels that’s hot by
definition, Flows divide into two types: cold and hot.
TYPES OF FLOW
Flow SharedFlow StateFlow
The basic abstraction of any A hot flow that can receive The type of SharedFlow
flow. Without implementation elements regardless of number where we always have a
details, you can only collect of consumers (there can even value present. We usually use
elements that can be cold or be no one). Has replayCache this type of flow in our mobile
hot (look for other types of to provide recent elements applications to observe state
flow for details). If it’s builtin (published before updates from UI-side.
with flow {} – it’s cold. subscribing) for new
consumers.
ChannelFlow
The cold type of Kotlin Flows
that uses Channel APIs in its
builders. The peculiarity of this
Flow subtype is that there is
no restriction on the context
of where we emit values into
our Flow (we can emit them
concurrently).
FLOW
When we talk about regular
flow, it’s usually done by a
several builders for different
use-cases. The most common one
is the flow {} builder (usually we
use it when we want to make
some kind of computations that
should be done lazily).