0

I was doing a project a while back where the user could choose a route/course and see its information on a separate page but got a problem regarding the extraction of the information of a specific entity stored in my Room database. I didn't end up finding the solution for the problem so I just changed my approach altogether and passed the necessary information through the intent that starts the activity instead of just getting the information by searching the database.

I've already tried to ask about the problem here How to get a specific Entity in Room but no one seems to see where the problem is so I'm changing the question.

What I want to know now is what is the best way to get a specific entity from Room database in Kotlin.

Migaloco
  • 31
  • 7
  • question is not clear, I cannot understand where is concrete problem. try to read this topic. https://medium.com/mindorks/room-kotlin-android-architecture-components-71cad5a1bb35 – Hayk Melkonyan Aug 06 '19 at 06:51

1 Answers1

0

I looked at your question and i think the issue is you trying to get the value of live data which usually returns null. I had a similar issue and i solved it but returning the actual entity from the DAO and reading it in the IO thread in the viewmodel

DAO:

 @Query("select * from groups WHERE groupid =:groupId ")
fun getGroup(groupId:String): Group

Repository:

   override suspend fun getGroup(groupId: String): Group {
  return   runBlocking {
        groupDao.getGroup(groupId)
    }
}

ViewModel:

  fun getGroup(groupID:String):Group{
    return runBlocking {
        async(Dispatchers.IO) {
            groupsRepository.getGroup(groupID)
        }.await()
    }

}
Bolu
  • 216
  • 3
  • 15