0
0
Kotlinprogramming~10 mins

FlowOn for changing dispatcher 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 switch the flow to the IO dispatcher.

Kotlin
flowOf(1, 2, 3).[1](Dispatchers.IO)
Drag options to blanks, or click blank then click option'
Acollect
BflowOn
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of flowOn.
Using map or filter which do not change dispatcher.
2fill in blank
medium

Complete the code to collect the flow on the Main dispatcher.

Kotlin
flowOf("A", "B", "C").flowOn(Dispatchers.IO).[1] { value -> println(value) }
Drag options to blanks, or click blank then click option'
Acollect
Bmap
Cfilter
DlaunchIn
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or filter which transform but do not collect.
Using launchIn which requires a CoroutineScope.
3fill in blank
hard

Fix the error in the code to correctly change the dispatcher.

Kotlin
val flow = flow {
    emit(10)
}.[1](Dispatchers.Default)

flow.collect { println(it) }
Drag options to blanks, or click blank then click option'
AflowOn
Bcollect
ClaunchIn
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of flowOn to change dispatcher.
Using launchIn without a CoroutineScope.
4fill in blank
hard

Fill both blanks to create a flow that emits numbers on IO dispatcher and collects on Main dispatcher.

Kotlin
flow {
    emit(1)
    emit(2)
}.[1](Dispatchers.IO).[2] { value -> println(value) }
Drag options to blanks, or click blank then click option'
AflowOn
Bmap
Ccollect
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or filter instead of collect to receive values.
Using collect before flowOn.
5fill in blank
hard

Fill all three blanks to create a flow that emits strings on Default dispatcher, maps them to uppercase, and collects on IO dispatcher.

Kotlin
flowOf("a", "b", "c").[1](Dispatchers.Default).[2] { it.[3]() }.collect { println(it) }
Drag options to blanks, or click blank then click option'
AflowOn
Bmap
Cuppercase
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter instead of map for transformation.
Using lowercase instead of uppercase.