如何在主线程上的onCreate/中获取房间列表的大小? [英] How to get size of Room list in onCreate/ on main thread?
问题描述
如何轻松获得数据库大小的计数,然后根据数据库是否为空来执行适当的操作?
How can I easily get the count of the database size so I can then do the appropriate actions based on whether the DB is empty or not?
我有一个App DB,View Model,Repository,Dao和所有其他组件,并且我的对象插入了...但是我无法调用onCreate()
数据库中列表的大小.当我尝试获取mAppDatabase.vehicleDao().getAll().getValue().size()
或mVehicleViewModel.getAll().getValue().size()
时,会出现空指针异常.
I have an App DB, View Model, Repository, Dao, and all the other pieces, and my objects insert... But I cannot call in onCreate()
the size of the list in the DB. When I ever try to get mAppDatabase.vehicleDao().getAll().getValue().size()
, or mVehicleViewModel.getAll().getValue().size()
, I get null pointer exceptions.
但是我知道我的对象正在插入,因为当我运行一个可观察对象时,我可以记录它们的信息...但是我无法在onCreate的主线程/上获得计数.帮助!下面的示例代码:
However I know my objects are inserting because when I run an observable, I can log their information... but I cannot get the count on the main thread/ in onCreate. Help! Example code below:
protected void onCreate(Bundle savedInstanceState) {
...
mAppDatabase = AppDatabase.getInstance(MyActivity.this);
Log.d("LISTSIZEAPP", String.valueOf(mAppDatabase.myDao().getAll().getValue().size()));
ObserveItems();
OR
protected void onCreate(Bundle savedInstanceState) {
...
mViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
Log.d("LISTSIZEVM", String.valueOf(mViewModel.getAll().getValue().size()));
ObserveItems();
两者的^ Null指针异常(尝试调用接口方法 空对象上的'java.lang.Object [] java.util.List.toArray()' 参考)...
^Null pointer exception for both (Attempt to invoke interface method 'java.lang.Object[] java.util.List.toArray()' on a null object reference)...
但是:
private void ObserveItems() {
mViewModel.getAll().observe(this, new Observer<List<Foo>>() {
@Override
public void onChanged(@Nullable final List<Foo> foos) {
mFoos= foos;
for (Vehicle v: mFoos) {
Log.d("ROOM INFO - FOOS", v.getFooTitle());
}
}
});
}
我可以记录我想要的所有信息.这样就可以清楚地插入项目了.是什么赋予了?我想念什么?谢谢.
I can log all the info I want. So the items are CLEARLY inserted. What gives? What am I missing? Thank you.
推荐答案
这是从LiveData同步访问数据:
This is accessing data synchronously from the LiveData:
Log.d("LISTSIZEVM", String.valueOf(mViewModel.getAll().getValue().size()));
但是此数据可能尚未到达.
But this data might no have arrived yet.
Try adding this query to your Dao, see google samples:
@Query("SELECT COUNT(column) FROM table")
int getDataCount();
正如@CommonsWare指出的那样,不能从主UI线程调用此方法,因为它是数据库操作,它将阻塞UI线程并引发错误.您可以在主线程上调用它,也可以返回LiveData<Integer>
并观察值,就像处理数据列表一样.
As pointed out by @CommonsWare this cannot be called from the main UI thread because it's a database opearation that will block the UI thread and will throw an error. You can either call this off the main thread, or return a LiveData<Integer>
and observe the value, like you did with the list of data.
@Query("SELECT COUNT(column) FROM table")
LiveData<Integer> getDataCount();
这篇关于如何在主线程上的onCreate/中获取房间列表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!