Challenge - 5 Problems
SharedFlow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when multiple collectors subscribe to a SharedFlow?
Consider a SharedFlow that emits integer events. If two collectors start collecting from this SharedFlow, what will they receive?
Android Kotlin
val sharedFlow = MutableSharedFlow<Int>() // Collector 1 sharedFlow.onEach { println("Collector 1 received: $it") }.launchIn(scope) // Collector 2 sharedFlow.onEach { println("Collector 2 received: $it") }.launchIn(scope) sharedFlow.emit(5)
Attempts:
2 left
💡 Hint
SharedFlow is designed to broadcast events to all active collectors.
✗ Incorrect
A SharedFlow emits events to all collectors that are actively collecting. So when you emit 5, both collectors receive it.
❓ lifecycle
intermediate2:00remaining
How does SharedFlow behave when collectors start after emission?
If a MutableSharedFlow is created with default settings and an event is emitted before any collector starts, what happens when a collector starts collecting later?
Android Kotlin
val sharedFlow = MutableSharedFlow<Int>() sharedFlow.emit(10) // Collector starts after emit sharedFlow.onEach { println("Received: $it") }.launchIn(scope)
Attempts:
2 left
💡 Hint
Default SharedFlow does not replay events to late collectors.
✗ Incorrect
By default, MutableSharedFlow does not replay past events to new collectors. So if the event was emitted before collecting started, the collector misses it.
📝 Syntax
advanced2:00remaining
What is the correct way to create a SharedFlow that replays the last event?
You want a SharedFlow that replays the last emitted event to new collectors. Which code snippet correctly creates such a SharedFlow?
Attempts:
2 left
💡 Hint
The parameter to control replay is named 'replay'.
✗ Incorrect
The replay parameter sets how many past events are replayed to new collectors. The correct parameter name is replay.
🔧 Debug
advanced2:00remaining
Why does this SharedFlow emit block suspend indefinitely?
Given this code, why does the call to
emit suspend forever?Android Kotlin
val sharedFlow = MutableSharedFlow<Int>(replay = 0, extraBufferCapacity = 0) // No collectors started sharedFlow.emit(1) // Suspends here
Attempts:
2 left
💡 Hint
Emit suspends if there is no buffer and no collector to receive the event.
✗ Incorrect
When replay and extraBufferCapacity are zero, emit suspends until a collector is ready to receive the event.
🧠 Conceptual
expert3:00remaining
How to ensure events are not lost when no collectors are active?
You want to use a SharedFlow to send UI events that must not be lost even if no collector is active at the moment of emission. Which configuration ensures this behavior?
Attempts:
2 left
💡 Hint
Replay buffers past events; extraBufferCapacity buffers new events when no collector is ready.
✗ Incorrect
Setting replay = 1 keeps the last event for new collectors. Setting extraBufferCapacity = 1 allows buffering one event when no collector is active, preventing suspension or loss.