Proposal: Flow.collectLatest
See original GitHub issueFunction signature
suspend fun <T> Flow<T>.collectLatest(action: suspend (value: T) -> Unit): Unit
Behavior
val flow = flowOf(1, 2, 3, 4, 5)
flow.collectLatest { value: Int -> // This lambda would be canceled if a new value is emitted although it hasn't been finished.
delay(100)
println(value) // So only '5' will be printed.
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:13 (8 by maintainers)
Top Results From Across the Web
When to use collect and collectLatest operator to collect kotlin ...
The crucial difference from collect is that when the original flow emits a new value then the action block for the previous value...
Read more >collectLatest - Kotlin
Terminal flow operator that collects the given flow with a provided action. The crucial difference from collect is that when the original flow...
Read more >Kotlin flows on Android - Android Developers
In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single...
Read more >A safer way to collect flows from Android UIs | by Manuel Vivo
In an Android app, Kotlin flows are typically collected from the UI ... If you use these APIs for flow collection, LiveData doesn't...
Read more >Kotlin Flows in Android summary - ProAndroidDev
Flow is the Kotlin type that can be used to model streams of data. ... You can use operators like collectLatest() for this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks. That is an interesting observation and it raises the whole set of issues with respect to naming. I wonder if we’ve making a mistaking of providing
switchMap
operator in the first place. Here I’ve recorded by thoughts: #1335@LouisCAD The ability to skip equals may be replaced by
Flow.distinctUntilChanged()
. In combination with it, you can do the same thing.For example: