Challenge - 5 Problems
Room Coroutine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you call a suspend DAO function from the main thread?
Consider a Room DAO suspend function called directly from the main thread without using any coroutine builder. What will happen?
Attempts:
2 left
💡 Hint
Room enforces thread rules for database access.
✗ Incorrect
Room requires database operations to be off the main thread. Calling a suspend DAO function directly on the main thread without coroutine context causes an IllegalStateException.
🧠 Conceptual
intermediate2:00remaining
Why use Flow in Room DAO with Coroutines?
What is the main advantage of returning a Flow from a Room DAO query function compared to a suspend function?
Attempts:
2 left
💡 Hint
Think about live data updates.
✗ Incorrect
Flow emits new data whenever the database table changes, allowing UI to reactively update. Suspend functions return a single snapshot of data.
❓ lifecycle
advanced2:00remaining
What happens if you collect a Flow from Room DAO without lifecycle awareness?
If you collect a Flow returned by a Room DAO in a coroutine launched in a ViewModel without tying it to lifecycle, what is a likely consequence?
Attempts:
2 left
💡 Hint
Consider what happens when UI components are destroyed.
✗ Incorrect
Without lifecycle-aware collection, the coroutine collecting the Flow keeps running, potentially leaking memory and wasting resources.
📝 Syntax
advanced2:00remaining
Which DAO function signature correctly uses suspend with Room?
Choose the correct Kotlin DAO function signature for inserting a User entity using coroutines.
Attempts:
2 left
💡 Hint
Room supports suspend functions returning inserted row id.
✗ Incorrect
The correct signature is a suspend function returning Long for the inserted row id. Deferred return type is not supported by Room DAO directly.
🔧 Debug
expert2:00remaining
Why does this coroutine collecting a Room Flow never emit data?
Given this code snippet in a ViewModel:
val usersFlow = userDao.getAllUsers()
init {
viewModelScope.launch {
usersFlow.collect { users ->
// update UI
}
}
}
The UI never updates even though the database has users. What is the most likely cause?
Android Kotlin
val usersFlow = userDao.getAllUsers()
init {
viewModelScope.launch {
usersFlow.collect { users ->
// update UI
}
}
}Attempts:
2 left
💡 Hint
Check DAO function annotations and return types.
✗ Incorrect
If the DAO function does not return a Flow (missing @Query or wrong return type), the Flow will never emit data and collection will not trigger updates.