Java POJO对象
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;
}
}
我的查询方法
@Query("SELECT * FROM section")
LiveData<List<Section>> getAllSections();
访问数据库
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
在下一行,我正在检查sections.getValue()
,它总是给我null,虽然我在DataBase中有数据,后来我在onChanged()
方法中得到了值。
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
}
});
但是当我从查询中省略LiveData时,我正在按预期获取数据。查询方法:
@Query("SELECT * FROM section")
List<Section> getAllSections();
访问数据库:
final List<Section> sections = mDb.sectionDAO().getAllSections();
我通过这种方法解决了这个问题
private MediatorLiveData<List<Section>> mSectionLive = new MediatorLiveData<>();
.
.
.
@Override
public LiveData<List<Section>> getAllSections() {
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
mSectionLive.addSource(sections, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sectionList) {
if(sectionList == null || sectionList.isEmpty()) {
// Fetch data from API
}else{
mSectionLive.removeSource(sections);
mSectionLive.setValue(sectionList);
}
}
});
return mSectionLive;
}
在下一行,我正在检查sections.getValue(),它总是给我null,虽然我在DataBase中有数据,后来我在onChanged()方法中得到了值。
这是一种正常行为,因为返回LiveData的查询正在异步工作。那一刻的值为null。
所以调用这个方法
LiveData<List<Section>> getAllSections();
你会在这里得到结果
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
}
});
来自文件:
除非您在构建器上调用allowMainThreadQueries(),否则Room不允许访问主线程上的数据库,因为它可能会长时间锁定UI。异步查询(返回LiveData或RxJava Flowable的查询)不受此规则的约束,因为它们在需要时在后台线程上异步运行查询。
如果sections.getValue()为null,我必须为数据调用api并插入数据库
您可以在onChange
方法处理此问题:
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
if(sections == null || sections.size() == 0) {
// No data in your database, call your api for data
} else {
// One or more items retrieved, no need to call your api for data.
}
}
});
但是你最好将这个数据库/表初始化逻辑放到存储库类中。看看Google's sample。请参阅DatabaseCreator课程。
我解决了类似的问题如下
在ViewModel类中
private LiveData<List<Section>> mSections;
@Override
public LiveData<List<Section>> getAllSections() {
if (mSections == null) {
mSections = mDb.sectionDAO().getAllSections();
}
return mSections;
}
这都是必需的。永远不要更改LiveData的实例。
LiveData是一个异步查询,您获取LiveData对象但它可能不包含任何数据。您可以使用额外的方法等待数据填充然后提取数据。
public static <T> T getValue(LiveData<T> liveData) throws InterruptedException {
final Object[] objects = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
Observer observer = new Observer() {
@Override
public void onChanged(@Nullable Object o) {
objects[0] = o;
latch.countDown();
liveData.removeObserver(this);
}
};
liveData.observeForever(observer);
latch.await(2, TimeUnit.SECONDS);
return (T) objects[0];
}