LiveData.getValue () devuelve nulo con Room
Objeto POJO Java
public class Section {
@ColumnInfo(name="section_id")
public int mSectionId;
@ColumnInfo(name="section_name")
public String mSectionName;
public int getSectionId() {
return mSectionId;
}
public void setSectionId(int mSectionId) {
this.mSectionId = mSectionId;
}
public String getSectionName() {
return mSectionName;
}
public void setSectionName(String mSectionName) {
this.mSectionName = mSectionName;
}
}
Mi método de consulta
@Query("SELECT * FROM section")
LiveData<List<Section>> getAllSections();
Accediendo a DB
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
En la siguiente línea estoy revisandosections.getValue()
que siempre me da un valor nulo aunque tengo datos en DataBase y luego obtengo el valor en elonChanged()
método.
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
}
});
Pero cuando omito LiveData de la consulta obtengo los datos como se esperaba. Método de consulta
@Query("SELECT * FROM section")
List<Section> getAllSections();
Accediendo a DB:
final List<Section> sections = mDb.sectionDAO().getAllSections();