#Flotlin
I'm starting this thread to explain @kotlin Flow🌊. Let's deep dive into Kotlin #Flow together🧑‍💻. Stay tuned with this thread.😎
#KotlinFlow #AndroidDev
#Flotlin 1

What is Flow?🤔
👉 A cold asynchronous data stream that sequentially emits values and completes normally or with an exception.

See this example:
pl.kotl.in/IPQaFgRTC?them…

#Kotlin #KotlinFlow Image
#Flotlin 2 - `map`
- `map` operator can be used on the upstream flow.
- It returns a downstream flow for further operations.

Try it here ⌨️: (Kotlin Playground)
pl.kotl.in/-EaVmBx8L?them…

#Kotlin #KotlinFlow Image
#Flotlin 3 - `filter`
- Similar to the collection's operator.
- Performs the operation on upstream flow.
- Returns a downstream flow containing only values of the original flow that matches the given predicate.

Try it here ⌨️:
play.kotlinlang.org/embed?short=fP…

#Kotlin #KotlinFlow Image
#Flotlin 4 - Context Preservation
- Flow encapsulates its own execution context and never propagates or leaks it downstream.
- Using the `flowOn()` operator changes the upstream context (Everything above `flowOn`).

Try it here ⌨️:
pl.kotl.in/2gJ2xHg4f

#Kotlin #KotlinFlow Image
#Flotlin 5 - flowOf()

- Creates a flow that produces values from specified arguments.

e.g. `flowOf(1, 2, 3)`

Try it here⌨️:
pl.kotl.in/eeNLeMRT-

#Kotlin #KotlinFlow Image
#Flotlin 6 - Exception Transparency
- Never wrap `emit()` with try and catch.
- Exception caused due to upstream flow can be handled using catch operator.

Try it here⌨️:
play.kotlinlang.org/embed?short=b8…

#Kotlin #KotlinFlow Image
#Flotlin 7 - `emitAll()`

- It collects all the values from the given flow and emits them to the collector.

- It's shorthand for
```
flow.collect { value -> emit(value) }
```

Try it here⌨️:
play.kotlinlang.org/embed?short=hM…

#Kotlin #KotlinFlow Image
#Flotlin 8 - `collectIndexed()`

- An operator which collects the given flow with the index of an element.
- In short, 1st emit -> 0, 2nd emit ->1 and so on..

Try it here⌨️:
play.kotlinlang.org/embed?short=aJ…

#Kotlin #KotlinFlow #Flow Image
#Flotlin 9 - `collectLatest()`

- When the original flow emits a new value, action block for previous value is cancelled.
- See below image, when Java, Scala and Kotlin are emitted finally, Kotlin is collected.

Try it here⌨️:
play.kotlinlang.org/embed?short=eg…

#Kotlin #KotlinFlow #Flow Image
#Flotlin 10 - `combine()`

- Combines the most recent emitted values by each flow.
- See below image and code (Kotlin Playground).

Try it here⌨️:
play.kotlinlang.org/embed?short=HZ…

#Kotlin #KotlinFlow #Flow Image
#Flotlin 11 - `combineTransform()`

- Combines the most recent emitted values by each flow.
- The receiver is FlowCollector. You can transform emitted elements.

Try it here⌨️:
play.kotlinlang.org/embed?short=yS…

#Kotlin #KotlinFlow #Flow Image
#Flotlin🌊 12 - `zip()`

- Zips value of current flow with other flow.
- Combines the values emitted by each flow.

Try it here⌨️:
play.kotlinlang.org/embed?short=P4…

#Kotlin #KotlinFlow #Flow In flowA, we're emitting 4 ...
#Flotlin🌊13 - `callbackFlow{}`

- It allows elements to be produced by code that is running in a different context or concurrently.
- It's useful when you're using listener which is an asynchronous operation.

Try it here⌨️:
play.kotlinlang.org/embed?short=mY…
#Kotlin #KotlinFlow #Reactive Image
#Flotlin🌊14 - StateFlow

- It is a flow which emits updates to its collectors.
- Value can be observed by collecting values from the flow.

Try it here⌨️:
pl.kotl.in/trmvjrOkU

#Kotlin #KotlinFlow #Reactive @kotlin @KotlinWeekly @HelloKotlin
@threader_app compile
#Flotlin🌊17: Subscription Count - StateFlow/SharedFlow

- StateFlow is now SharedFlow (By inheritance)
- Subscription count of these hot streams can be observed using property `subscriptionCount`.
- By this, we can know active or inactive subscribers.

#Kotlin #Flow #StateFlow Image
#Flotlin🌊18: Replacing LiveData with SharedFlow

- Coroutine library provided an extension on Flow: `shareIn()`.
- By using it we can use hot flows like a cold one❄️.
- It's configurable🛠️.
- Here's an example to use SharedFlow instead of LiveData 👇

#Kotlin #Flow #AndroidDev Image

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Shreyas Patil 🇮🇳👨‍💻

Shreyas Patil 🇮🇳👨‍💻 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!