Skip to content

collectLatest issue. it's working as expected only after a delay statement. #3188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Manideepkota0708 opened this issue Feb 13, 2022 · 7 comments
Labels

Comments

@Manideepkota0708
Copy link

fun main() = runBlocking<Unit> {
    val mutableSharedFlow = MutableSharedFlow<String>()
    val sharedFlow: SharedFlow<String> = mutableSharedFlow

    launch {
        sharedFlow.collectLatest {
            println("launch1: $it")
        }
    }

    launch {
        mutableSharedFlow.emit("ONE")
        mutableSharedFlow.emit("TWO")
        mutableSharedFlow.emit("THREE")
        mutableSharedFlow.emit("FOUR")
        mutableSharedFlow.emit("FIVE")
        delay(10)
        mutableSharedFlow.emit("SIX")
        mutableSharedFlow.emit("SEVEN")
        mutableSharedFlow.emit("EIGHT")
        mutableSharedFlow.emit("NINE")
        mutableSharedFlow.emit("TEN")
    }
}

output:
launch1: SIX
launch1: SEVEN
launch1: EIGHT
launch1: NINE
launch1: TEN

@Manideepkota0708
Copy link
Author

why the first five events are skipped?

@LouisCAD
Copy link
Contributor

Because that's how collectLatest works. It only cares about the latest, and cancels collection of a previous value, if any.

If you don't want this behavior, use collect.

@LouisCAD
Copy link
Contributor

I think it's already quite clear in the collectLatest KDoc, or did something read ambiguous or unclear to you?

@Manideepkota0708
Copy link
Author

the first five emits are not getting suspended even if there is a collectLatest attached to the shared flow.
the other five emits are suspending and resuming.

The example given in kotlin docs is pretty easy to understand as it has delay method inside the collectLatest block, hence it is canceling the existing one and accepting the next one.

but I didn't get why SIX, SEVEN, EIGHT, NINE, TEN events are getting collected missing the first five events.

I understand that using collect I'm able to get without any issues, but I just thought this should also work the same way.

you can close this if it's not an issue @LouisCAD. And thanks for your patience and response (every time you are replying as soon as you see the comment).

@darylcrypto
Copy link

darylcrypto commented Apr 3, 2024

Hi @qwwdfsad i am facing the exact same problem but couldnt know if kotlin team has worked on it, or which version is being affected. Do you mind give some advice?

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Apr 3, 2024

Hey, could you please elaborate on the exact problem you are having?

@Manideepkota0708
Copy link
Author

Hai @qwwdfsad, firstly kotlin is a great language, I use it every day for my android development.
please refer this issue
Pls do put some time and copy the exact code in intelli-j and see the output, and If possible explain me.
If it's not at all an issue, just ignore. Thanks for your time @qwwdfsad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants