Android Room + AsyncTask

Viewed 15670

My team have developed a new Android app which makes extensive use of Room.

I am unsure whether we are using AsyncTask correctly.

We have had to wrap all calls to insert/update/delete in AsyncTasks which results in a huge number of AsyncTasks. All the calls into Room are from background services. There is no direct Room access from activities or fragments - they get everything via LiveData.

An example call to insert a row:

AsyncTask.execute(() -> myModelDAO.insertInstance(myModel));

With this in the DAO:

@Insert
void insertInstance(MyModel model);
4 Answers

All the calls into Room are from background services

Then you should not be using AsyncTask. AsyncTask is only for when you want to do work on the main application thread after doing background work, and that is almost never the case with a service. Use something else (thread, thread pool, RxJava, etc.). This has nothing specific to do with Room.

Related