-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Most of the times sharedFlow doesn't emit the initial value #2338
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
Comments
I have noticed that if I add a delay(100) on the onStart it works fine everytime |
Have you tried onSubscription mentioned in #2034 ?
Is that the case? |
Hi @diousk Thanks for your reply. I had a look at it when you mentioned |
The Does it help? |
Hi @elizarov yes I got that thanks. But that's still an issue for me as that onStart/OnSubscribe action if made on the upstream. I think it would help if the first value received by a sharedFlow was waiting for it initialisation before consuming it. Would that be possible to do? Thanks! |
@CoreFloDev Can you, please, explain in more detail what are you trying to achieve. Please, give some more background. What kind of events you are sending? How many consumers of those events your application architecture has? Maybe you are looking for a |
@CoreFloDev are you possibly running into a similar behavior I was initially confused by as well? #2069 (comment) |
@CoreFloDev Can you, please, share some more snippets of code -- a full example of the code for one use-case would be great. |
Sure the code lives there: https://github.com/CoreFloDev/flowarch/blob/main/app/src/main/java/com/example/flow_arch/main/arch/MainScreen.kt, I have make the repository public as it is only a prototype for a migration from rxjava to flow. The overall structure takes the shape of an existing rxjava implementation. The usecases implementation are available here: Let me know if that helps, thanks! |
In your particular architecture the easiest solution I see if to inject
This way, every subscriber is guaranteed to get |
That's similar to the |
In general -- no. The design of the flow is very simple and it does not support the concept of "subscription" in general case. For an arbitrary flow you cannot tell when the "subscription" had happened, so that's why |
Hi, My friend Pawel also got that issue, he found a way around my using a merge like this: fun inputToAction() = FlowTransformer<MainInput, Action> { flow ->
merge(flowOf(Action.InitialAction),
flow.map { input ->
when (input) {
MainInput.Click -> Action.IncrementNumber
MainInput.RetryClicked -> Action.InitialAction
}
})
} Just posting the solution for future reference in case someone get the same issue Erratum: Nan doesn't work still |
Hi,
I have been testing sharedFlow over the weekend and I got a strange behaviour with it.
I have got some code like that:
If I debug using doOnEach, I was able to see that the Action.InitialAction is emitted every time. However after the shareIn, the stream is not always setup and the function loadMovieListUseCase doesn't receive that InitialAction. And the strange part, it is that the next event on the stream, always respond properly. I have tried both SharingStarted.Lazily and Eagerly, it seems that eagerly make it worth.
Maybe I am missing something, note that I have also tried with coroutine 1.4.0 and it didn't fixed the issue.
Thank you for your time!
The text was updated successfully, but these errors were encountered: