dimsuz
09/27/2022, 5:46 PMval shared = MutableSharedFlow<Int>()
merge(
shared.onSubscription { shared.emit(3) },
flowOf(100,200,300),
).collect {
println("$it")
}
never prints 3, only 100 200 300?
And is there a way to setup this so that I have some point where it is safe to post to shared to guarantee delivery to collect?Andreas Scheja
09/27/2022, 7:25 PMshared.onSubscription { emit(3) }
instead of
shared.onSubscription { shared.emit(3) }
actually works as expected. I think your problem is that onSubscription doesn't modify shared but returns a new SharedFlow instead. so shared.emit(3) emits 3 to a flow that is actually never subscribed?dimsuz
09/28/2022, 10:03 AMshared, because it can have other subscribers. This sample is a minimal reporduction of the issue I have on a larger scale.
soThat's not the case, because it's passed toemitsshared.emit(3)to a flow that is actually never subscribed?3
merge and merge will subscribe to it.
Nevertheless I'm interested to know why doesn't this work as it is :)natario1
09/29/2022, 4:34 PMemit(3) . Instead of calling onSubscription for every subscriber, you should save it somewhere: val shared2 = shared.onSubscription { emit(…) } and have subscribers subscribe to shared2