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);







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




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế