I just started using android room. Only problem is, It takes several layers for db interaction. Table Class -> Dao Interface -> Database Repo -> ViewModels
And in each layer, code repetition is there.
As if I directly call queries from Repo, without viewModels, it will not allow. Because call without viewModel observer becomes synchronous, which blocks main thread.
Either there must be standard way to call repo asynchronously, or some hacks. May be we can use some Async generic class, which lets you pass queries and return result to main thread.
Possible hack. Don't knwo if it is correct way.
AsyncTask.execute(new Runnable() {
@Override
public void run() {
List<User> users = apiService.getAllUsers();
if(users.size()>0)
{
System.out.println("Total users:"+users.size());
System.out.println("Email:"+users.get(0).getEmail());
}
}
});
You can use an AsyncTask for this without the need for ViewModels.
AsyncTask.execute {
val entity = daoInterface.queryFunction()
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With