#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:
https://pl.kotl.in/IPQaFgRTC?theme=darcula&from=4

#Kotlin #KotlinFlow
#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)
https://pl.kotl.in/-EaVmBx8L?theme=darcula

#Kotlin #KotlinFlow
#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 ⌨️:
https://play.kotlinlang.org/embed?short=fPFvt0ZHZ&theme=darcula

#Kotlin #KotlinFlow
#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 ⌨️:
https://pl.kotl.in/2gJ2xHg4f 

#Kotlin #KotlinFlow
#Flotlin 5 - flowOf()

- Creates a flow that produces values from specified arguments.

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

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

#Kotlin #KotlinFlow
#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⌨️:
https://play.kotlinlang.org/embed?short=b8wSIrJrB

#Kotlin #KotlinFlow
#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⌨️:
https://play.kotlinlang.org/embed?short=hM8W3ZpjS

#Kotlin #KotlinFlow
#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⌨️:
https://play.kotlinlang.org/embed?short=aJGLTmZ5Z

#Kotlin #KotlinFlow #Flow
#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⌨️:
https://play.kotlinlang.org/embed?short=egwXoO8xo&theme=darcula

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

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

Try it here⌨️:
https://play.kotlinlang.org/embed?short=HZh1dFfS7&theme=darcula

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

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

Try it here⌨️:
https://play.kotlinlang.org/embed?short=ySsOutYqT

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

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

Try it here⌨️:
https://play.kotlinlang.org/embed?short=P4hu3E2ot

#Kotlin #KotlinFlow #Flow
#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⌨️:
https://play.kotlinlang.org/embed?short=mY095T0hT
#Kotlin #KotlinFlow #Reactive
#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⌨️:
https://pl.kotl.in/trmvjrOkU 

#Kotlin #KotlinFlow #Reactive @kotlin @KotlinWeekly @HelloKotlin
@threader_app compile
You can follow @imShreyasPatil.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: