Does it every make sense for me to need two Dao fu...
# room
v
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.
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
Can you just get the first element in the
LiveData
instead?
b
I use two functions in my app for this situation - it works fine for me
r
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
Both dao functions have their use cases. I see similar approaches with coroutines where you have 2 identical queries:
suspend fun
vs
Flow