0
0
Kotlinprogramming~10 mins

SharedFlow for event broadcasting in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a MutableSharedFlow instance.

Kotlin
val eventFlow = MutableSharedFlow<[1]>()
Drag options to blanks, or click blank then click option'
ADouble
BInt
CString
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the event data.
Forgetting to specify the generic type.
2fill in blank
medium

Complete the code to emit an event into the SharedFlow.

Kotlin
suspend fun sendEvent(event: String) {
    eventFlow.[1](event)
}
Drag options to blanks, or click blank then click option'
Acollect
Bemit
Claunch
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect which is for receiving events.
Using launch which is for coroutines, not flows.
3fill in blank
hard

Fix the error in collecting events from the SharedFlow.

Kotlin
suspend fun observeEvents() {
    eventFlow.[1] { event ->
        println("Received: $event")
    }
}
Drag options to blanks, or click blank then click option'
Acollect
Bemit
Csend
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using emit which is for sending events.
Using subscribe which is not a Kotlin Flow method.
4fill in blank
hard

Fill both blanks to create a SharedFlow with replay and buffer capacity.

Kotlin
val bufferedFlow = MutableSharedFlow<String>(replay = [1], extraBufferCapacity = [2])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Setting replay or buffer to zero when some buffering is needed.
Confusing replay and buffer parameters.
5fill in blank
hard

Fill all three blanks to filter and map events in a SharedFlow.

Kotlin
val filteredFlow = eventFlow
    .filter { it.length [1] [2] }
    .map { it.[3]() }
Drag options to blanks, or click blank then click option'
A>
B3
Cuppercase
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Using a non-existent string method.