question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Flow.collectLatest

See original GitHub issue

Function 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Jul 15, 2019

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

1reaction
BoxResincommented, Jun 14, 2019

@LouisCAD The ability to skip equals may be replaced by Flow.distinctUntilChanged(). In combination with it, you can do the same thing.

For example:

val flow = flowOf(1, 2, 2, 3).delayEach(100)
flow.distinctUntilChanged().collectLatest {
    // Do something
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found