Android 房间数据库Livedata getValue()返回null

Android 房间数据库Livedata getValue()返回null,android,viewmodel,dao,android-room,android-livedata,Android,Viewmodel,Dao,Android Room,Android Livedata,我试图使用LiveData和viewmodel从房间中获取自定义对象数据列表。使用Livedata的getValue()方法时,返回null,但getting list直接显示实际数据。如何在viewmodel类中使用LiveData获取时段类的列表 实体类 获取viewmodel类中的数据方法 private void getDataFromDatabase(){ 新建异步任务(){ @凌驾 受保护的LiveData doInBackground(无效…无效){ LiveData period

我试图使用LiveData和viewmodel从房间中获取自定义对象数据列表。使用Livedata的getValue()方法时,返回null,但getting list直接显示实际数据。如何在viewmodel类中使用LiveData获取时段类的列表

实体类

获取viewmodel类中的数据方法

private void getDataFromDatabase(){
新建异步任务(){
@凌驾
受保护的LiveData doInBackground(无效…无效){
LiveData periodList=MyApplication.getDatabase().dao().getAllPeriodList();
//句点periodList=MyApplication.getDatabase().dao().getWriterList(EndPoints.OLD\u ENGLISH\u句点);
返回周期列表;
}
@凌驾
受保护的void onPostExecute(LiveData避免){
super.onPostExecute(避免);
//Period Period=aVoid.getValue().get(0);
Log.d(“header”,aVoid.getValue().get(0.getHeader());
}
}.execute();
}

通常
Room
以同步方式处理查询
LiveData
使用它,这些查询将以异步方式执行,因此当您调用此行时:

LiveData<List<Period>> periodList = MyApplication.getDatabase().dao().getAllPeriodList();
内部视图

public LiveData<List<Period>> getAllPeriods() {
    return MyApplication.getDatabase().dao().getAllPeriodList();
}
viewModel.getAllPeriods().observe(this, new Observer<List<Period>>() {
    @Override
    public void onChanged(@Nullable List<Period> periods) {
        updateList(periods); 
    }
});
viewModel.getAllPeriods().observe(这是一个新的观察者(){
@凌驾
更改后的公共void(@Nullable List periods){
更新列表(周期);
}
});
事实是

  • Asynctask
    正在后台的另一个线程上异步运行
  • Livedata
    像漂浮在数据库上一样工作以查看更改
  • 因此,完成
    Asyntask
    的获取操作需要时间。您可以通过在10秒延迟的处理程序内测试
    Asynctask
    来查看它。
    因此,最好在视图模型中直接调用fetch操作,并将其作为livedata发送到activity/fragment。注意。

    不要使用
    AsyncTask
    livedata.getValue()。。。使用
    LiveData.observe()
    newasynctask(){
    你是从哪里得到这个想法的?我很确定这不是文档中描述的。根据我的想法,获取操作应该以异步方式调用,这就是我犯错误的原因。顺便说一句,感谢大家澄清我的想法。我们是否应该继续使用
    异步任务
    ?感谢您的解释,现在已经完全解决了我很清楚。@Razon欢迎您,我也提供了代码示例,希望对您有所帮助。我已经用handler进行了测试,它是真实的。再次感谢您的解释。通过asynctask可以更好地了解房间操作。@Razon但请不要使用handler,正如Hossain所说的,将livedata传递给您的视图类并观察it@keivan,我告诉你se处理程序仅用于测试,以查看实际发生的情况。
    @Database(version = 1, entities = {Period.class})
    public abstract class MyDatabase extends RoomDatabase{
    
    static MyDatabase databaseInstance;
    
    public static MyDatabase getDatabaseInstance(Context context) {
        if (databaseInstance == null)
            databaseInstance = Room
                    .databaseBuilder(context, MyDatabase.class, "testDatabase1")
                    .fallbackToDestructiveMigration()
                    //     .addMigrations(FROM_1_TO_2)
                    .build();
    
        return databaseInstance;
    }
    
    abstract public DAO dao();
    
    
    
    }
    
     private void getDataFromDatabase() {
    
        new AsyncTask<Void, Void, LiveData<List<Period>>>() {
            @Override
            protected LiveData<List<Period>> doInBackground(Void... voids) {
                LiveData<List<Period>> periodList = MyApplication.getDatabase().dao().getAllPeriodList();
              //  Period periodList = MyApplication.getDatabase().dao().getWriterList(EndPoints.THE_OLD_ENGLISH_PERIOD);
                return periodList;
            }
    
            @Override
            protected void onPostExecute(LiveData<List<Period>> aVoid) {
                super.onPostExecute(aVoid);
             //  Period period = aVoid.getValue().get(0);
                Log.d("header", aVoid.getValue().get(0).getHeader());
            }
        }.execute();
    
    }
    
    LiveData<List<Period>> periodList = MyApplication.getDatabase().dao().getAllPeriodList();
    
    public LiveData<List<Period>> getAllPeriods() {
        return MyApplication.getDatabase().dao().getAllPeriodList();
    }
    
    viewModel.getAllPeriods().observe(this, new Observer<List<Period>>() {
        @Override
        public void onChanged(@Nullable List<Period> periods) {
            updateList(periods); 
        }
    });