Does it every make sense for me to need two Dao functions which return the same data, except one is
LiveData
and the other is not? Because I think I need that.
v79
11/19/2022, 9:45 AM
Specifically, when I call the
init { ]
block in my ViewModel, I need to fetch a fixed value from the database. Currently, the database returns a LiveData object, as I need it like that elsewhere. So I think I'm going to end up in the state where my DAO and repo has the following almost-duplicate functions:
Copy code
@Query("SELECT * FROM ChargeEvent WHERE id = :id")
fun getChargeEventWithId(id: Long): LiveData<ChargeEvent?>
@Query("SELECT * FROM ChargeEvent WHERE id = :id")
fun getExistingChargeEventWithId(id: Long): ChargeEvent?
Which just looks like a code smell to me. Or rather, a lack-of-understanding smell.
m
Michael Marshall
11/21/2022, 3:08 AM
Can you just get the first element in the
LiveData
instead?
b
Brandon Howard
11/30/2022, 6:20 PM
I use two functions in my app for this situation - it works fine for me
r
rattleshirt
12/14/2022, 3:00 PM
You could also only have a function which returns a
Flow
and call
viewModelScope.launch { flow.firstOrNull() }
from
init
if you only need it once. Otherwise you could always extract the string from the query as const and reuse it in both
rattleshirt
12/14/2022, 3:03 PM
Both dao functions have their use cases. I see similar approaches with coroutines where you have 2 identical queries: