0
0
Android Kotlinmobile~20 mins

Room with Flow for reactive data in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you collect a Flow from Room DAO?
You have a Room DAO function returning Flow<List<User>>. What is the behavior when you collect this Flow in your UI?
Android Kotlin
interface UserDao {
  @Query("SELECT * FROM users")
  fun getAllUsers(): Flow<List<User>>
}
AThe Flow emits null values when the database is empty.
BThe Flow emits data only once and then completes.
CYou must manually refresh the Flow to get new data.
DThe UI receives updates automatically whenever the users table changes.
Attempts:
2 left
💡 Hint
Think about how Flow works with Room to provide live updates.
lifecycle
intermediate
2:00remaining
How to safely collect a Room Flow in an Android Activity?
You want to collect a Flow from Room in an Activity to update UI. Which lifecycle-aware approach is best?
AUse lifecycleScope.launchWhenStarted { dao.getAllUsers().collect { ... } }
BUse runBlocking { dao.getAllUsers().collect { ... } } in onCreate.
CCall dao.getAllUsers().collect() directly on the main thread.
DUse GlobalScope.launch { dao.getAllUsers().collect { ... } }
Attempts:
2 left
💡 Hint
Consider Android lifecycle and coroutine scopes.
📝 Syntax
advanced
2:00remaining
What is the correct DAO function signature for a Flow query?
Choose the correct Kotlin function signature for a Room DAO that returns a Flow of a list of entities named Item.
Afun getItems(): Flow<Item>
Bfun getItems(): List<Flow<Item>>
Cfun getItems(): Flow<List<Item>>
Dfun getItems(): LiveData<Flow<List<Item>>>
Attempts:
2 left
💡 Hint
Think about the type that represents a stream of lists.
🔧 Debug
advanced
2:00remaining
Why does collecting a Room Flow not emit updates?
You collect a Flow from Room DAO but the UI never updates after initial data. What is a likely cause?
Android Kotlin
val usersFlow = userDao.getAllUsers()
lifecycleScope.launchWhenStarted {
  usersFlow.collect { users ->
    adapter.submitList(users)
  }
}
AThe database table was not updated, so no new emissions occur.
BYou forgot to call collectLatest instead of collect.
CRoom requires manual refresh calls to emit new data.
DThe Flow must be converted to LiveData to emit updates.
Attempts:
2 left
💡 Hint
Consider what triggers Flow emissions from Room.
🧠 Conceptual
expert
2:00remaining
How does Room with Flow handle backpressure and cancellation?
When collecting a Flow from Room, how does Room handle backpressure and cancellation to avoid resource leaks?
ARoom buffers all database changes and emits them all even if the collector is slow.
BRoom cancels the query and stops emitting when the collector cancels the Flow collection.
CRoom requires manual cancellation of queries to avoid leaks.
DRoom converts Flow to LiveData internally to handle backpressure.
Attempts:
2 left
💡 Hint
Think about how Flow and Room cooperate with coroutine cancellation.