Bird
Raised Fist0
Android Kotlinmobile~10 mins

Room with Flow for reactive data in Android Kotlin - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using Flow with Room in Android apps?
easy
A. It makes the database queries run faster than normal queries.
B. It allows the app to automatically update UI when database data changes.
C. It encrypts the database data for security.
D. It reduces the app size by removing unused code.

Solution

  1. Step 1: Understand Flow's role in Room

    Flow provides a stream of data that emits updates when the database changes.
  2. Step 2: Connect Flow to UI updates

    Collecting Flow in a coroutine lets the UI reactively update without manual refresh.
  3. Final Answer:

    It allows the app to automatically update UI when database data changes. -> Option B
  4. Quick Check:

    Flow = reactive UI updates [OK]
Hint: Flow streams data changes to UI automatically [OK]
Common Mistakes:
  • Thinking Flow speeds up queries
  • Confusing Flow with encryption
  • Assuming Flow reduces app size
2. Which of the following is the correct way to declare a DAO function that returns a Flow list of users?
easy
A. fun getUsers(): List<User>
B. fun getUsers(): LiveData<User>
C. fun getUsers(): Flow
D. fun getUsers(): User

Solution

  1. Step 1: Identify return type for reactive data

    Flow is the correct type for a reactive stream of user lists.
  2. Step 2: Compare other options

    List<User> is static, LiveData<User> is not Flow, User is a single object.
  3. Final Answer:

    fun getUsers(): Flow<List<User>> -> Option C
  4. Quick Check:

    Flow return type = fun getUsers(): Flow [OK]
Hint: Use Flow for reactive list queries [OK]
Common Mistakes:
  • Returning List instead of Flow
  • Confusing LiveData with Flow
  • Returning single object instead of list
3. Given this DAO function:
fun getAllTasks(): Flow<List<Task>>

What happens when you collect this Flow in your UI and a new task is inserted into the database?
medium
A. The Flow emits the updated list including the new task automatically.
B. The Flow stops emitting data until restarted.
C. The Flow emits only the new task, not the full list.
D. The Flow throws an exception because of data change.

Solution

  1. Step 1: Understand Flow behavior on data change

    Flow emits new data whenever the underlying database query result changes.
  2. Step 2: Apply to task insertion

    Inserting a new task triggers Flow to emit the updated full list including the new task.
  3. Final Answer:

    The Flow emits the updated list including the new task automatically. -> Option A
  4. Quick Check:

    Flow emits updated list on insert [OK]
Hint: Flow emits full updated data on DB changes [OK]
Common Mistakes:
  • Thinking Flow stops on data change
  • Expecting only new item emission
  • Assuming Flow throws error on insert
4. You wrote this DAO function:
@Query("SELECT * FROM users")
fun getUsers(): List<User>

You want reactive updates but your UI does not update after data changes. What is the problem?
medium
A. The function should return Flow instead of List<User>.
B. The @Query annotation is missing.
C. You need to call the function inside a coroutine.
D. The database must be closed before updates.

Solution

  1. Step 1: Identify return type issue

    Returning List<User> gives a static snapshot, no reactive updates.
  2. Step 2: Fix return type for reactive data

    Changing return type to Flow enables reactive updates on data change.
  3. Final Answer:

    The function should return Flow<List<User>> instead of List<User>. -> Option A
  4. Quick Check:

    Return Flow for reactive updates [OK]
Hint: Return Flow to get reactive data updates [OK]
Common Mistakes:
  • Forgetting to use Flow return type
  • Missing @Query annotation (not the case here)
  • Not using coroutine to collect Flow
5. You want to display a list of notes that updates automatically when the database changes. Which combination correctly implements this using Room and Flow?
1. DAO function: ?
2. Collect Flow in UI: ?
hard
A. 1. fun getNotes(): List<Note> 2. Call getNotes() once in onCreate()
B. 1. fun getNotes(): Flow<Note> 2. Collect Flow without coroutine
C. 1. fun getNotes(): LiveData 2. Observe LiveData with observe()
D. 1. fun getNotes(): Flow 2. lifecycleScope.launch { dao.getNotes().collect { updateUI(it) } }

Solution

  1. Step 1: Choose correct DAO return type

    Flow<List<Note>> provides reactive stream of note lists.
  2. Step 2: Collect Flow properly in UI

    Use lifecycleScope.launch to collect Flow and update UI reactively.
  3. Final Answer:

    1. fun getNotes(): Flow<List<Note>> 2. lifecycleScope.launch { dao.getNotes().collect { updateUI(it) } } -> Option D
  4. Quick Check:

    Flow + coroutine collection = reactive UI [OK]
Hint: Use Flow in DAO and collect in coroutine for reactive UI [OK]
Common Mistakes:
  • Returning List instead of Flow
  • Not collecting Flow in coroutine
  • Confusing LiveData with Flow
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard