LiveData
当我的 httprequest 成功时,我的对象被调用了两次,但如果发生错误,它只被调用一次,导致 UI 显示一个空列表,因为我的验证Error
被跳过,因为没有触发警报。
我的存储库代码
private LiveData<List<Card>> getCards() {
return cardsDao.getCards();
}
// Network request, that deals with the success or error of the request
public LiveData<DataWrapper<List<Card>>> loadCards() {
AtomicReference<LiveData<DataWrapper<List<Card>>>> atomicWrapper = new AtomicReference<>();
if (getCards().getValue() == null) {
webService.getCards(result -> {
CardResponse res = (CardResponse) result;
List<Card> cardList = res.getCardsList();
getInsertAsyncTask.execute(cardList.toArray(new Card[cardList.size()]));
}, error -> {
atomicWrapper.set(asLiveData(null, error.getMessage()));
}, TAG);
}
atomicWrapper.set(asLiveData(getCards(),null));
return atomicWrapper.get();
}
我的 BaseRepository 代码
LiveData<DataWrapper<List<T>>> asLiveData(LiveData<List<T>> dataSource, String error) {
MediatorLiveData<DataWrapper<List<T>>> mediatorLiveData = new MediatorLiveData<>();
mediatorLiveData.addSource(dataSource, data -> mediatorLiveData.setValue(new DataWrapper<>(data, error)));
return mediatorLiveData;
}
我的片段代码
private void subscribeToCards() {
mViewModel.getCards().observe(this, listDataWrapper -> {
if( listDataWrapper == null ) {
return;
}
if( listDataWrapper.error != null) {
// Show error on UI
dismissProgress();
Log.e(TAG, "Error - " + listDataWrapper.error);
showError(getString(R.string.cards_error_get_list_message));
EventBus.getDefault().post(new DialogMessageEvent(getString(R.string.cards_error_get_list_title),
getString(R.string.cards_error_get_list_message), getString(R.string.cards_error_get_list_button)));
}
if( listDataWrapper.data != null ) {
// Update Ui
refreshCardsList(listDataWrapper.data);
cardsViewVisibility(true);
}
});
}
最后,我的 ViewModel 代码
public LiveData<DataWrapper<List<Card>>> getCards(){
return repository.loadCards();
}
总而言之,在失败的情况下,为什么observer callback
only 会被调用一次?因为我已经对其进行了调试,并且在两种情况下(成功和失败),该方法asLiveData
都称为 TWICE,但只有在成功的尝试中回调也被称为 TWICE,失败时observer callback
它仅称为 ONCE。
编辑:添加了异步任务代码
@SuppressLint("StaticFieldLeak")
AsyncTask<T,Void,Void> getInsertAsyncTask = new AsyncTask<T, Void, Void>() {
@Override
protected Void doInBackground(T... ts) {
daoObject.insertObjects(Arrays.asList(ts));
return null;
}
};