I'm trying to figure out the best way to fetch a single row via Room by ID. I have copied code from a guide on how to fetch all my rows in an entity (Workout
plus all the joined entities) and that works great for populating a recyclerview.
But when I need to drill down to just one, currently, my guess code in the repository is
@WorkerThread
suspend fun getWorkoutById(id: Long): LiveData<WorkoutTree> {
return workoutDao.getWorkoutById(id)
}
and in the DAO
@Transaction
@Query("SELECT * FROM WORKOUTS WHERE id=:id")
fun getWorkoutById(id: Long): LiveData<WorkoutTree>
Naturally I call repository.getWorkoutById(myId)
from my ViewModel so I can populate the data and reference it from my Activity/Fragment.
But I'm trying to figure out whether I'm on the right track and the best way to be fetching this. It's a DB local to the phone, so shouldn't be issues of needing live updates, honestly.
Would it be better to just serialize the object tapped in the Recyclerview listing all rows and pass that into my activity and not bother to fetch by ID?
Pretty much every example I find online about Room and Android development gives examples for fetching all rows and use LiveData and the repository has a public property that is LiveData> populated by an initial DAO fetch of all rows from the entity.
I'm having difficulty translating this example mentally into one where I'd just fetch one row.
I don't really have any functioning code to share since I can't really figure out how to architect this in Kotlin and Room and such. I've never written an Android app before and come from a functional programming background via Python, JS, Scala and so am struggling with how Kotlin guides and Android guides recommend you do this with all the additional classes and interfaces used to decouple everything for a phone app.