I have an app based on MVVM, There are 5 Dao interfaces:
@Dao
interface DataDaoOne {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertData(classA: ClassA) : Long
@Query("SELECT * FROM class_a")
fun getData() : LiveData<List<ClassA>>
}
Similarly, the second Dao, and five more.
@Dao
interface DataDaoTwo {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertData(classB: ClassB) : Long
@Query("SELECT * FROM class_a")
fun getData() : LiveData<List<ClassB>>
}
I access the DAOs using a repository, what I want to do is, create a function that calls all the getData() from the five DAOs and stores the values in a variable. It will wait until the data from all tables are fetched then create another data class that will hold all data fetched from the tables and return it.
E.g:
data class AllData(
val classA: List<ClassA>,
val classB: List<ClassB>,
)
getAllData() : AllData{
val dataA = repository.getClassAData()
val dataB = repository.getClassBData()
return AllData(dataA, dataB)
}
Now I can make an API call and pass this AllData class object to it.
// Fetching the data and storing it in a variable
val allData = getAllData()
// Passing the all data to API calling function.
sendDataToServer(allData)
The problem I am facing is calling the DAO functions from the repository and await for them to fetch the data for all tables, how can be solve this issue?