solru
solru

Reputation: 151

stateIn operator doesn't update the cached value of StateFlow

I have some shared flows which are merged into several flows in different classes and then these flows are merged into a single one. The original shared flows are configured as such:

protected val statusMutable = MutableSharedFlow<Int>()
val status: Flow<Int> = statusMutable.asSharedFlow()

So, the default configuration. In the end I have code like this:

val status
    get() = merge(
        object1.status, // merged shared flows
        object2.status, // merged shared flows
        object3.status, // merged shared flows
        statusMutable
    ).stateIn(GlobalScope, SharingStarted.Lazily, initialValue = someInt)

This flow should be infinite hot flow so I'm using GlobalScope (you can also tell me how to approach this better if there's better way but that's not the actual problem).

The actual problem is that values are emitted and collected fine but the resulting StateFlow doesn't update its value property (therefore the replay cache) for some reason. This leads to a situation where a new subscriber doesn't get the latest emitted value on subscription. What can be the cause of this and how can I fix it?

Upvotes: 1

Views: 726

Answers (1)

solru
solru

Reputation: 151

It turns out the problem is that I access this flow through a getter though I just needed to assign it to the property. Such a stupid mistake! What's funny, I gave up on finding the answer myself and posted this question only to notice a mistake after just like 15 minutes.

Upvotes: 7

Related Questions