Skip to content

shared flow collectLatest not working as expected #3162

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 Jan 29, 2022 · 8 comments
Closed

shared flow collectLatest not working as expected #3162

Manideepkota0708 opened this issue Jan 29, 2022 · 8 comments

Comments

@Manideepkota0708
Copy link

sample 1:

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

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

    launch {
        mutableSharedFlow.emit("ONE")
        mutableSharedFlow.emit("TWO")
    }

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

output :
launch1: ONE
launch1: TWO
launch2: TWO


sample 2:

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")
    }

    launch {
        sharedFlow.collect {
            println("launch2: $it")
        }
    }

}

output:(nothing)


sample 3:

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

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

    launch {
        mutableSharedFlow.emit("ONE")
        mutableSharedFlow.emit("TWO")
    }

    launch {
        sharedFlow.collect {
            println("launch2: $it")
        }
    }

}

output: (i didn't understand why this is the order )
launch2: ONE
launch1: ONE
launch1: TWO
launch2: TWO

@LouisCAD
Copy link
Contributor

Aren't the 3 "samples" all the same? 🤔

@Manideepkota0708
Copy link
Author

Aren't the 3 "samples" all the same? 🤔

output is different, I have printed output either.

@LouisCAD
Copy link
Contributor

But why copy exactly the same code 3 times? This is just adding noise, and decreases the likelihood that people will try to understand the issue you have.

This, plus referencing this issue in other unrelated issues is not the best way to attract the attention you're looking for.

For this issue, I suggest that you make a new one that clearly shows the expected output and the actual output, without duplicating code unnecessarily, and maybe by being thorough about this, you'll find out, before hitting the submit button, whether it's an actual issue in kotlinx.coroutines, or if your understanding was incomplete and improved in the process.

Personally speaking, it happened to me a few times that I was writing an issue, and in the process, realized my mistake or how I was having incorrect expectations.

@LouisCAD
Copy link
Contributor

Last thing, please, don't quote the message you're replying to when it's the message just above, I get that Kotlin's philosophy is to be explicit, but sometimes, in natural language, we can't save some lines to read 😉

@Manideepkota0708
Copy link
Author

each sample is different,

  • sample1: launch1 contains collect, launch2 contains collect latest
  • sample2: launch1 contains collectLatest, launch2 contains collect
  • sample3: launch1 and launch2 contains collect

for both sample1 and sample2, I expected the same output which didn't happen.
with little effort, I was able to understand sample 1 and sample 3

can you explain why it doesn't print anything in sample2? @LouisCAD

@Manideepkota0708
Copy link
Author

created a new one #3188 @LouisCAD

@BarryFruitman
Copy link

BarryFruitman commented Dec 8, 2022

@Manideepkota0708 It sucks when someone doesn't read your question and railroads your thread. 😢

This one was extra fun:

Aren't the 3 "samples" all the same? 🤔

output is different, I have printed output either.

But why copy exactly the same code 3 times?

@Manideepkota0708
Copy link
Author

I even created a new one #3188, he has given an explanation but didn't get it what he was saying. Just left it @BarryFruitman

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

No branches or pull requests

3 participants