#Flotlin
I& #39;m starting this thread to explain @Kotlin Flow
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🌊" title="Wasserwelle" aria-label="Emoji: Wasserwelle">. Let& #39;s deep dive into Kotlin #Flow together
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧑💻" title="Technologist" aria-label="Emoji: Technologist">. Stay tuned with this thread.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="😎" title="Lächelndes Gesicht mit Sonnenbrille" aria-label="Emoji: Lächelndes Gesicht mit Sonnenbrille">
#KotlinFlow #AndroidDev
I& #39;m starting this thread to explain @Kotlin Flow
#KotlinFlow #AndroidDev
#Flotlin 1
What is Flow?
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🤔" title="Denkendes Gesicht" aria-label="Emoji: Denkendes Gesicht">
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> 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
https://pl.kotl.in/IPQaFgRTC... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
What is Flow?
See this example:
https://pl.kotl.in/IPQaFgRTC?theme=darcula&from=4
https://pl.kotl.in/IPQaFgRTC... href="https://twtext.com//hashtag/Kotlin"> #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
https://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">: (Kotlin Playground)
https://pl.kotl.in/-EaVmBx8L?theme=darcula
https://pl.kotl.in/-EaVmBx8L... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- `map` operator can be used on the upstream flow.
- It returns a downstream flow for further operations.
Try it here
https://pl.kotl.in/-EaVmBx8L?theme=darcula
https://pl.kotl.in/-EaVmBx8L... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
#Flotlin 3 - `filter`
- Similar to the collection& #39;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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=fPFvt0ZHZ&theme=darcula
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- Similar to the collection& #39;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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://pl.kotl.in/2gJ2xHg4f
https://pl.kotl.in/2gJ2xHg4f... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- 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
https://pl.kotl.in/2gJ2xHg4f... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
#Flotlin 5 - flowOf()
- Creates a flow that produces values from specified arguments.
e.g. `flowOf(1, 2, 3)`
Try it here
https://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://pl.kotl.in/eeNLeMRT-
https://pl.kotl.in/eeNLeMRT-... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- Creates a flow that produces values from specified arguments.
e.g. `flowOf(1, 2, 3)`
Try it here
https://pl.kotl.in/eeNLeMRT-
https://pl.kotl.in/eeNLeMRT-... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=b8wSIrJrB
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
#Flotlin 7 - `emitAll()`
- It collects all the values from the given flow and emits them to the collector.
- It& #39;s shorthand for
```
flow.collect { value -> emit(value) }
```
Try it here
https://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=hM8W3ZpjS
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow
- It collects all the values from the given flow and emits them to the collector.
- It& #39;s shorthand for
```
flow.collect { value -> emit(value) }
```
Try it here
https://play.kotlinlang.org/embed?short=hM8W3ZpjS
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=aJGLTmZ5Z
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=egwXoO8xo&theme=darcula
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=HZh1dFfS7&theme=darcula
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=ySsOutYqT
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
#Flotlin
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🌊" title="Wasserwelle" aria-label="Emoji: Wasserwelle"> 12 - `zip()`
- Zips value of current flow with other flow.
- Combines the values emitted by each flow.
Try it here
https://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=P4hu3E2ot
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
- 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
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Flow
#Flotlin
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🌊" title="Wasserwelle" aria-label="Emoji: Wasserwelle">13 - `callbackFlow{}`
- It allows elements to be produced by code that is running in a different context or concurrently.
- It& #39;s useful when you& #39;re using listener which is an asynchronous operation.
Try it here
https://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://play.kotlinlang.org/embed?short=mY095T0hT
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Reactive
- It allows elements to be produced by code that is running in a different context or concurrently.
- It& #39;s useful when you& #39;re using listener which is an asynchronous operation.
Try it here
https://play.kotlinlang.org/embed?short=mY095T0hT
https://play.kotlinlang.org/embed... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Reactive
#Flotlin
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🌊" title="Wasserwelle" aria-label="Emoji: Wasserwelle">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://abs.twimg.com/emoji/v2/... draggable="false" alt="⌨️" title="Tastatur" aria-label="Emoji: Tastatur">:
https://pl.kotl.in/trmvjrOkU
https://pl.kotl.in/trmvjrOkU... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Reactive @kotlin @KotlinWeekly @HelloKotlin
- 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
https://pl.kotl.in/trmvjrOkU... href="https://twtext.com//hashtag/Kotlin"> #Kotlin #KotlinFlow #Reactive @kotlin @KotlinWeekly @HelloKotlin
@threader_app compile