What does MutableSharedFlow.subscriptionCount give you, and what is resetReplayCache used for? Sketch a use case.
answer
- subscriptionCount: StateFlow<Int> of active collectors
- Use it for reference-counted upstream (start/stop)
- shareIn(WhileSubscribed) automates this
- resetReplayCache() clears cached replay values
- resetReplayCache no-op when replay=0
basics
~20 ssubscriptionCount is a StateFlow of how many collectors are active right now. resetReplayCache empties the cache of recent values so new subscribers don't get old ones. You use subscriptionCount to start/stop upstream work on demand.
solid answer
~40 sMutableSharedFlow exposes subscriptionCount: StateFlow<Int>, a live count of active collectors. You can collect it to drive reference-counting: start an expensive upstream source (sensor, websocket, polling) when the count goes from 0 to >0 and stop it when it returns to 0. This is essentially what shareIn(SharingStarted.WhileSubscribed()) automates. resetReplayCache() clears the replay cache so future subscribers won't receive previously cached values — useful when cached values become stale or irrelevant (e.g. after a logout or screen reset) and you don't want a late subscriber to replay them. Note resetReplayCache does not emit anything to current collectors and has no effect when replay = 0. Together they let you manage a hot source's lifecycle and the freshness of what late joiners observe.
go deeper
Can say subscriptionCount counts collectors and resetReplayCache clears cached values.
Knows subscriptionCount is a StateFlow<Int> and that resetReplayCache affects only future subscribers.
Wires reference-counted upstream lifecycle via subscriptionCount and explains stale-cache resets, relating both to shareIn.
Designs hot-source lifecycle and cache-freshness policy (logout/reset boundaries) and decides when manual reference counting beats shareIn.
## subscriptionCount ```kotlin val subscriptionCount: StateFlow<Int> ``` A `StateFlow<Int>` reflecting how many collectors are **currently** subscribed. Because it is itself a StateFlow you can observe transitions: ```kotlin scope.launch { sharedFlow.subscriptionCount .map { it > 0 } .distinctUntilChanged() .collect { active -> if (active) startSensor() else stopSensor() } } ``` This is **reference counting**: spin up an expensive upstream only while someone listens. The `shareIn` operator with `SharingStarted.WhileSubscribed()` builds on exactly this idea. ## resetReplayCache ```kotlin fun resetReplayCache() ``` Clears the **replay cache** — the up-to-`replay` most recent values that new subscribers would otherwise replay. After calling it, a freshly subscribing collector sees nothing from before the reset. - It does **not** push anything to already-subscribed collectors. - It is a no-op when `replay == 0` (there is nothing to clear). - It is **not** valid / meaningful on `StateFlow` (a StateFlow always has a current value). ## Use case A `MutableSharedFlow<UserProfile>(replay = 1)` caches the last loaded profile so a late screen gets it instantly. On **logout** you call `resetReplayCache()` so the next subscriber doesn't replay the previous user's profile. Meanwhile `subscriptionCount` drives whether the profile-refresh job runs. ```kotlin class ProfileBus(scope: CoroutineScope) { private val _profile = MutableSharedFlow<UserProfile>(replay = 1) val profile: SharedFlow<UserProfile> = _profile.asSharedFlow() init { scope.launch { _profile.subscriptionCount .map { it > 0 }.distinctUntilChanged() .collect { if (it) refreshLoop() } } } fun onLogout() = _profile.resetReplayCache() } ```
- How does subscriptionCount relate to shareIn?shareIn(SharingStarted.WhileSubscribed()) internally watches subscription count to start/stop the upstream, so you rarely wire it by hand.
- Does resetReplayCache notify existing collectors?No. It only affects what future subscribers replay; current collectors are unaffected.
saying these in an interview costs you the question
- Thinking subscriptionCount is a plain Int snapshot rather than a StateFlow<Int>
- Believing resetReplayCache emits a value to collectors
- Claiming resetReplayCache works on StateFlow
- Not realizing resetReplayCache is a no-op at replay=0