type
Post
status
Published
date
Apr 30, 2024
slug
kotlin-flow-test
summary
Immediately
start a new coroutine/switch context after each collector updates its value, without affecting the next collector.tags
Kotlin
Futures
category
技术分享
icon
password
Start
Code
Output
Conclusion
MutableSharedFlowis ahotflow, which means it will emit the value to all the collectors.
- All collectors of
MutableSharedFloware in the same coroutine when receiving values, which means that once one collector is blocked (delay), no other collector will receive the value!
Test and Verify
Code
Output
Conclusion
- All collectors are executed in the same coroutine and are not in the same coroutine as the
emit's coroutine.
- The first time it was a different coroutine was because the collector was not registered at the same time.
Overthrow
Code
Output
Conclusion
- Each collector is indeed in different coroutines.
Why does blockage cause other collectors to be unable to receive properly?
It is highly likely that when updating the value to the collector, he actually initiated a coroutine. However, he forcibly waited for the coroutine to end before pushing the value to the next collector.
Solve
Immediately
start a new coroutine/switch context after each collector updates its value, without affecting the next collector.