Everything works (Insert, Update, Delete with AsyncTask) fine, but getting a single row by id not...
The main problem is that there are two Activities.
- A RecyclerView
- Details page
I would like to get one item by id from the Database after clicked on a RecyclerView list item. If I get it with LiveData I can not synchronize it with a variable in the UI thread.
How can you get a single row from Database?
in the DAO I have:
@Query("SELECT * FROM codes WHERE id = :id")
Code getCodeById(int id);
in the Repository:
public Code retrieveCode(int id){
return myDatabase.getCodeDao().getCodeById(id);
}
If I write it in the main thread
selectedCode = mCodeRepository.retrieveCode(codeId);
it gives an error:
Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Should I use AsyncTask, but how?