SharedFlow lets many parts of your app listen to the same events at the same time. It helps send messages that everyone can hear.
SharedFlow for event broadcasting in Kotlin
val sharedFlow = MutableSharedFlow<Type>(replay = 0) // To send an event sharedFlow.emit(value) // To listen for events sharedFlow.collect { value -> // handle value }
MutableSharedFlow is used to create a flow you can send events to.
The replay parameter controls how many past events new listeners get.
val sharedFlow = MutableSharedFlow<String>(replay = 0) // Emit an event sharedFlow.emit("Hello") // Collect events sharedFlow.collect { message -> println("Received: $message") }
val sharedFlow = MutableSharedFlow<Int>(replay = 2) // Emit some numbers sharedFlow.emit(1) sharedFlow.emit(2) sharedFlow.emit(3) // New collector will receive last 2 events: 2 and 3
This program shows two listeners receiving events from a SharedFlow. The second listener starts after some events were emitted but still receives the last event because replay is 1.
import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun main() = runBlocking { val sharedFlow = MutableSharedFlow<String>(replay = 1) // First listener launch { sharedFlow.collect { value -> println("Listener 1 received: $value") } } // Emit events sharedFlow.emit("Event 1") sharedFlow.emit("Event 2") delay(100) // Wait a bit // Second listener starts late launch { sharedFlow.collect { value -> println("Listener 2 received: $value") } } sharedFlow.emit("Event 3") delay(100) // Wait to see output }
SharedFlow is hot, meaning it keeps running even if no one listens.
Use replay carefully to avoid memory issues with many events.
Collecting from SharedFlow should be done in a coroutine.
SharedFlow lets you send events to many listeners at once.
Listeners can get past events if replay is set.
It is useful for broadcasting messages or user actions in apps.
Practice
SharedFlow in Kotlin?Solution
Step 1: Understand SharedFlow's role
SharedFlow is designed to send events to many listeners at once, like a radio broadcast.Step 2: Compare with other options
It does not store data permanently, perform heavy computations, or create single-threaded queues.Final Answer:
To broadcast events to multiple listeners simultaneously -> Option AQuick Check:
SharedFlow = Broadcast events [OK]
- Confusing SharedFlow with data storage
- Thinking SharedFlow handles computations
- Assuming SharedFlow is single-threaded queue
SharedFlow with replay of 2 events?Solution
Step 1: Recall SharedFlow creation syntax
MutableSharedFlow constructor uses parameterreplayto set how many past events to keep.Step 2: Check options for correct parameter
Only val flow = MutableSharedFlow<Int>(replay = 2) usesreplay = 2correctly. Others use wrong parameter names or types.Final Answer:
val flow = MutableSharedFlow<Int>(replay = 2) -> Option BQuick Check:
Use replay parameter to set past events [OK]
- Using SharedFlow instead of MutableSharedFlow for creation
- Wrong parameter names like bufferSize or replayCount
- Confusing replay with buffer
val flow = MutableSharedFlow<Int>(replay = 1)
suspend fun main() {
flow.emit(10)
flow.emit(20)
flow.collect { value ->
println(value)
}
}Solution
Step 1: Understand replay behavior
Withreplay = 1, only the last emitted value is kept for new collectors.Step 2: Analyze emitted values and collection
Values 10 and 20 are emitted, but only 20 is replayed to the collector.Final Answer:
20 -> Option CQuick Check:
Replay 1 means last event only [OK]
- Expecting all emitted values to be replayed
- Confusing replay with buffer size
- Thinking collector receives events emitted before subscription
val flow = MutableSharedFlow<Int>()
suspend fun main() {
flow.collect { println(it) }
flow.emit(5)
}Solution
Step 1: Understand collect and emit behavior
collect is a suspending function that waits for events and blocks until cancelled.Step 2: Analyze code flow
Since collect is called first and blocks, emit is never reached. emit must be called concurrently.Final Answer:
collect should be launched in a separate coroutine -> Option AQuick Check:
collect blocks; emit needs separate coroutine [OK]
- Calling emit after collect in same coroutine
- Assuming emit runs before collect automatically
- Thinking replay parameter is mandatory
SharedFlow. Which approach correctly ensures all components receive the latest 3 clicks even if they subscribe late?Solution
Step 1: Identify replay requirement
Replay of 3 means new subscribers get last 3 events automatically.Step 2: Match with SharedFlow usage
MutableSharedFlow withreplay = 3fits perfectly for broadcasting recent events to multiple listeners.Step 3: Evaluate other options
MutableStateFlow holds only one value, Channel does not replay past events, List is manual and not reactive.Final Answer:
Create a MutableSharedFlow with replay = 3 and emit clicks; components collect from it -> Option DQuick Check:
Replay 3 in SharedFlow broadcasts last 3 events [OK]
- Using StateFlow which holds only one value
- Using Channel which doesn't replay past events
- Manually managing event lists instead of reactive streams
Ready to prove it?
Pass the test and this pattern moves unknown
