Room Database Query
Room Database Query
I'm new to Room
and i'm trying to query
my database
to get a row
from it. I attempted doing so by querying
it with the primary key
which is id but the problem is i don't know how to return the target object
from the repository
.
Room
query
database
row
querying
primary key
object
repository
This is the Dao
Dao
@Query("SELECT * FROM targets WHERE id = :id LIMIT 1")
Targets findTargetById(int id);
THis is the Repository class
public Targets findTarget (int id)
new findTargetByIDAsyncTask(mTargetsDao).execute(id);
private static class findTargetByIDAsyncTask extends AsyncTask<Integer, Void, Targets>
private TargetsDao mAsyncTaskDao;
findTargetByIDAsyncTask(TargetsDao dao)
mAsyncTaskDao = dao;
@Override
protected Targets doInBackground(Integer... integers)
return mAsyncTaskDao.findTargetById(integers[0]);
@Override
protected void onPostExecute(Targets targets)
super.onPostExecute(targets);
2 Answers
2
You have two ways to return a result.
The first way is to call AsyncTask.get()
method, but it will still hold a MainThread what leads to ANR if a task will longer than 5 seconds:
AsyncTask.get()
public Targets findTarget (int id)
return new findTargetByIDAsyncTask(mTargetsDao).execute(id).get();
The second way is more complicated but it will not hold the MainThread. You should add a Callback class:
public interface Callback
void onSuccess(Targets targets);
Each method of your repository will look like that:
public void findTarget (Callback callback, int id)
new findTargetByIDAsyncTask(mTargetsDao, callback).execute(id);
And AsynTask will look like that:
private static class FindTargetByIDAsyncTask extends AsyncTask<Integer, Void, Targets>
private final TargetsDao mAsyncTaskDao;
private final Callback callback;
FindTargetByIDAsyncTask(TargetsDao dao, Callback callback)
mAsyncTaskDao = dao;
this.callback = callback;
@Override
protected Targets doInBackground(Integer... integers)
return mAsyncTaskDao.findTargetById(integers[0]);
@Override
protected void onPostExecute(Targets targets)
callback.onSuccess(targets);
The point is to get the data/object from a background thread. You can use Android's AsyncTask
or a ExecutorService
. A simple example is if you want to get a String of a user name the method will be:
AsyncTask
ExecutorService
private String getName() ExecutionException e)
e.printStackTrace();
return name;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you can do anything you want with your targets in the onPostExecute methode of your repository. if you want to use it anywhere out of your assyncTask then you can create a global variable and affect the value in onPostExecute
– kfir88
Sep 9 '18 at 9:19