0
0
Android Kotlinmobile~10 mins

Room with Flow for reactive data in Android 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 declare a Flow that observes all users from the DAO.

Android Kotlin
fun getAllUsers(): [1]<List<User>>
Drag options to blanks, or click blank then click option'
AFlow
BObservable
CList
DLiveData
Attempts:
3 left
💡 Hint
Common Mistakes
Using LiveData instead of Flow when Flow is required.
Returning a plain List instead of a reactive stream.
2fill in blank
medium

Complete the code to collect data from the Flow in a coroutine scope.

Android Kotlin
lifecycleScope.launch {
  userDao.getAllUsers().[1] { users ->
    // update UI with users
  }
}
Drag options to blanks, or click blank then click option'
Aobserve
Bsubscribe
Ccollect
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using observe which is for LiveData, not Flow.
Trying to subscribe like RxJava instead of collect.
3fill in blank
hard

Fix the error in the DAO function to return a Flow of users.

Android Kotlin
@Query("SELECT * FROM users")
fun getUsers(): [1]<List<User>>
Drag options to blanks, or click blank then click option'
AList
BLiveData
CObservable
DFlow
Attempts:
3 left
💡 Hint
Common Mistakes
Returning List directly which is not reactive.
Using LiveData instead of Flow in Kotlin coroutines.
4fill in blank
hard

Fill both blanks to create a Flow that filters users older than 18.

Android Kotlin
fun getAdultUsers(): [1]<List<User>> = userDao.getAllUsers()
  .[2] { list -> list.filter { it.age > 18 } }
Drag options to blanks, or click blank then click option'
AFlow
Bmap
Cfilter
DLiveData
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter on Flow which filters emissions, not list items.
Returning LiveData instead of Flow.
5fill in blank
hard

Fill the blanks to debounce rapid Flow emissions and collect them.

Android Kotlin
lifecycleScope.launch {
  userDao.getAllUsers()
    .[1](300)
    .[2] { users ->
      // update UI
    }
}
Drag options to blanks, or click blank then click option'
Adebounce
BcollectLatest
Ccollect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect without debounce causing too many updates.
Using map instead of debounce for timing control.