Shared Flow and State Flow
In this series of articles , we are discussing about Kotlin flows and in this article we will discuss about State Flow and Shared Flow.
Shared Flow
shares emitted value to all collectors in a broadcast way
all collectors receives all emitted values
This flow is active regardless of active collectors
You can customize the SharedFlow
behavior in the following ways:
replay
lets you resend a number of previously-emitted values for new subscribers.onBufferOverflow
lets you specify a policy for when the buffer is full of items to be sent. The default value isBufferOverflow.SUSPEND
, which makes the caller suspend. Other options areDROP_LATEST
orDROP_OLDEST
.
Code gist can be found in https://gist.github.com/vprabhu/adf153e7f3cbe6f5c39a4efe2c5d3400
State Flows
emits a single updated value to its collectors
current value can be accessed using
value
property
update{...}
thread safe function to change the state
excepts a block of code and this code block result is updated to
value
property which turns to be the updated value and this valueis emitted to all collectors
Code gist can be found in https://gist.github.com/vprabhu/3473f8c69375ba6271b57d426fcfacfd
Please leave your comments to improve and discuss more
Happy and Enjoy coding
Subscribe to my newsletter
Read articles from Vignesh Prabhu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vignesh Prabhu
Vignesh Prabhu
I am an Android application developer who is looking for new challenges to solve , love to learn and implement new things in coding