如何在Android中使用没有LiveData的房间和数据库? [英] How to use room and databases without LiveData in android?
问题描述
我是新来的房间,我想为我的应用程序建立一个数据库.我浏览了关于developer.android.com上的空间的纪录片.我写了一个使用LiveData的小应用程序,它运行良好.每次数据库更改时,基础数据模型也会更改.
但是对于新应用程序,我不需要LiveData功能.我有一组存储在数据库中的数据,应用程序无法更改.因此,它或多或少是静态的.如何在不使用LiveData概念的情况下从数据库中检索数据?
这将是第一个猜测:
dao.getAllItems();
据我所知,查询不应该在主线程上.所以我必须将其放在asyncTask中:
公共类存储库{私人dbDao mDao;私人List< Items>mAllItems;公共存储库(应用程序申请){DB db = DB.getDatabase(应用程序);mDao = db.dao();新的queryAsyncTask(mDao).execute(mAllItems);}私有静态类queryAsyncTask扩展了AsyncTask< List< Items>,Void,Void>.{私有dbDao mAsyncTaskDao;queryAsyncTask(dbDao dao){mAsyncTaskDao = dao;}@Override受保护的Void doInBackground(List< Items> ... params){params [0] = mAsyncTaskDao.getAllItems();返回null;}}}
这使我想到下一个问题.如果我同时触发两个或多个查询,则需要一种机制来确保所有asyncTasks都完成,就像onFinishedAllLoadingListener一样.做这个的最好方式是什么?还是有一种更简单的方法来解决我的问题,而无需将每个查询都包装在单独的asyncTask中?
如何在不使用LiveData概念的情况下从数据库中检索数据?
不要使用 LiveData
作为DAO方法的返回类型.
据我所知,查询不应该在主线程上.所以我必须将其放入asyncTask
使用 LiveData
,Kotlin协程或RxJava比 AsyncTask
更好.考虑到此代码似乎位于存储库中而不与UI绑定,即使是普通的 Thread
或单线程 Executor
也会更好.
尤其是,只要您只将 AsyncTask
与 doInBackground()
一起使用,就无需首先使用 AsyncTask
,与简单的线程相比.使用 AsyncTask
的 only 原因是因为您需要 onPostExecute()
在主应用程序线程上做一些工作.
而且,由于认为 AsyncTask
已过时,所以我真的鼓励您使用其他方法.Room具有对 LiveData
,Kotlin协程和RxJava的内置支持.
如果我同时触发两个或多个查询,则需要一种机制来确保所有asyncTasks都完成,就像onFinishedAllLoadingListener一样.最好的方法是什么?
在您的情况下,只需将它们放在一个线程中即可.由于 AsyncTask
默认情况下使用单个后台线程,因此您的代码无论如何不会同时有两个或多个查询.
关于其他可能的解决方案:
-
对于Kotlin协程,
async()
和await()
是最简单的选择 -
对于RxJava,如EpicPandaForce所建议,请使用
zip()
运算符
I am new to room and I wanna set up a database for my app. I walked through the documentary for room on developer.android.com. I wrote a little app which uses LiveData and it works fine. Everytime the database changes the underlying data model changes as well.
But for a new app I don't need the LiveData feature. I have a set of data stored in the database which cannot be altered by the app. So it's more or less static. How can I retrieve data from my database without using the LiveData concept?
This would be a first guess:
dao.getAllItems();
As far as I know queries shouldn't be on the main-thread. So I would have to put it in an asyncTask:
public class Repository {
private dbDao mDao;
private List<Items> mAllItems;
public Repository(Application application) {
DB db = DB.getDatabase(application);
mDao = db.dao();
new queryAsyncTask(mDao).execute(mAllItems);
}
private static class queryAsyncTask extends AsyncTask<List<Items>, Void, Void> {
private dbDao mAsyncTaskDao;
queryAsyncTask(dbDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(List<Items>... params) {
params[0] = mAsyncTaskDao.getAllItems();
return null;
}
}
}
That brings me to the next question. If I fire two or more queries at the same time I need a mechanism to ensure that all asyncTasks have finished like a onFinishedAllLoadingListener. What is the best way to do this? Or is there any easier method to solve my Problem without wrapping each query in a separate asyncTask?
How can I retrieve data from my database without using the LiveData concept?
Don't use LiveData
as a return type from your DAO methods.
As far as I know queries shouldn't be on the main-thread. So I would have to put it in an asyncTask
It would be much better for you to use LiveData
, Kotlin coroutines, or RxJava than AsyncTask
. Even a plain Thread
or single-thread Executor
would be better, given that this code seems to be in a repository and not tied to the UI.
In particular, any time you use AsyncTask
with only doInBackground()
, there was no need to use AsyncTask
in the first place, compared to a simple thread. The only reason to use AsyncTask
is because you need onPostExecute()
to do some work on the main application thread.
And, since AsyncTask
is considered to be obsolete, I really encourage you to use something else. Room has built-in support for LiveData
, Kotlin coroutines, and RxJava.
If I fire two or more queries at the same time I need a mechanism to ensure that all asyncTasks have finished like a onFinishedAllLoadingListener. What is the best way to do this?
In your case, just put them in a single thread. Your code will not have two or more queries at the same time anyway, as AsyncTask
uses a single background thread by default.
In terms of other likely solutions:
With Kotlin coroutines, probably
async()
andawait()
are the simplest optionsWith RxJava, as EpicPandaForce suggested, use the
zip()
operatorWith
LiveData
, use a library that offers azip()
operator, or create your own
这篇关于如何在Android中使用没有LiveData的房间和数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!