What is Flow in Kotlin and why is it useful with Room?
Flow is a Kotlin type that can emit multiple values sequentially. It is useful with Room because it allows your app to reactively observe database changes and update the UI automatically.
Click to reveal answer
beginner
How do you define a DAO method to return reactive data using Flow?
In your DAO interface, declare a function that returns Flow<List<Entity>> instead of a direct list. For example: @Query("SELECT * FROM users") fun getUsers(): Flow<List<User>>
Click to reveal answer
intermediate
What happens when the data in the Room database changes if you use Flow?
The Flow automatically emits the new data to all collectors. This means your UI or other parts of the app get updated data without manual refresh calls.
Click to reveal answer
intermediate
How do you collect data from a Flow in a ViewModel?
You can collect the Flow inside a coroutine scope, often using viewModelScope.launch. Then you update a LiveData or StateFlow to expose data to the UI.
Click to reveal answer
advanced
Why is using Flow with Room better than returning LiveData?
Flow is more flexible and supports many operators for data transformation. It integrates well with Kotlin coroutines and allows better control over threading and cancellation.
Click to reveal answer
Which return type should a DAO method use to provide reactive streams of data?
AFlow<List<Entity>>
BList<Entity>
CLiveData<Entity>
DEntity
✗ Incorrect
Flow> allows the DAO to emit updates reactively whenever the data changes.
What Kotlin feature does Flow work with to handle asynchronous data streams?
AHandlers
BThreads
CCallbacks
DCoroutines
✗ Incorrect
Flow is built on Kotlin coroutines to provide asynchronous and reactive data streams.
How do you start collecting data from a Flow in a ViewModel?
ADirectly calling flow.collect() on the main thread
You collect Flow inside a coroutine launched in viewModelScope to safely handle lifecycle and threading.
What happens if the Room database data changes when using Flow?
ANothing, you must refresh manually
BThe app crashes
CFlow emits the updated data automatically
DThe data is cached and not updated
✗ Incorrect
Flow emits new data automatically when the database changes, enabling reactive UI updates.
Which of these is NOT an advantage of using Flow with Room?
ARequires manual UI refresh
BIntegrates with Kotlin coroutines
CAllows cancellation and threading control
DSupports operators for data transformation
✗ Incorrect
Flow automatically updates collectors; manual UI refresh is not needed.
Explain how Room and Flow work together to provide reactive data updates in an Android app.
Think about how data changes in the database reach the UI without manual refresh.
You got /4 concepts.
Describe the steps to collect data from a Room Flow in a ViewModel and expose it to the UI.
Focus on coroutine usage and data exposure patterns.
You got /4 concepts.
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
Step 1: Understand Flow's role in Room
Flow provides a stream of data that emits updates when the database changes.
Step 2: Connect Flow to UI updates
Collecting Flow in a coroutine lets the UI reactively update without manual refresh.
Final Answer:
It allows the app to automatically update UI when database data changes. -> Option B
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
Step 1: Identify return type for reactive data
Flow is the correct type for a reactive stream of user lists.
Step 2: Compare other options
List<User> is static, LiveData<User> is not Flow, User is a single object.
Final Answer:
fun getUsers(): Flow<List<User>> -> Option C
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
Step 1: Understand Flow behavior on data change
Flow emits new data whenever the underlying database query result changes.
Step 2: Apply to task insertion
Inserting a new task triggers Flow to emit the updated full list including the new task.
Final Answer:
The Flow emits the updated list including the new task automatically. -> Option A
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
Step 1: Identify return type issue
Returning List<User> gives a static snapshot, no reactive updates.
Step 2: Fix return type for reactive data
Changing return type to Flow enables reactive updates on data change.
Final Answer:
The function should return Flow<List<User>> instead of List<User>. -> Option A
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
Step 1: Choose correct DAO return type
Flow<List<Note>> provides reactive stream of note lists.
Step 2: Collect Flow properly in UI
Use lifecycleScope.launch to collect Flow and update UI reactively.
Final Answer:
1. fun getNotes(): Flow<List<Note>>
2. lifecycleScope.launch { dao.getNotes().collect { updateUI(it) } } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.