在网络请求失败的情况下,实时数据不会触发两次



当我的 httprequest 成功时,我的LiveData对象被调用了两次,但在发生错误的情况下,它只被调用一次,导致 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();
}

我的基本存储库代码

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);
}
});
}

最后,我的视图模型代码

public LiveData<DataWrapper<List<Card>>> getCards(){
return repository.loadCards();
}

总而言之,万一失败,为什么observer callback只被调用一次?因为我已经调试了它,并且在两种(成功和失败(情况下,方法asLiveData被调用两次,但只有在成功尝试中,回调也被调用两次,失败时observer callback它只被调用一次。

编辑:添加了异步任务代码

@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;
}
};

您获得成功案例的 2 次回调而错误场景仅收到 1 次调用的原因似乎与您的存储库设置有关。

调用 loadCards 时,首先通过此调用发出数据库的状态:

atomicWrapper.set(asLiveData(getCards(),null));

此时将查询您的数据库,当前值将触发mediatorLiveData.setValue。这将是第一次排放。

mediatorLiveData.addSource(dataSource, data -> mediatorLiveData.setValue(new DataWrapper<>(data, error)));

同时,您已经触发了对 Web 服务的调用,如果成功,将触发您的异步任务以更新数据库。

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);

一旦插入命令完成,MediatorLiveData 将再次触发它的 setValue 调用 - 它正在侦听数据库中的更改,因此在插入时它将收到回调。这是成功案例中的第二次排放。

在错误情况下,通过null作为数据源传递。令人惊讶的是,这不会崩溃,因为 addSource 方法将源参数标记为非。由于它是空的,所以你不会得到回调,也不会调用mediatorLiveData.setValue。这意味着对于错误场景,您只收到第一个发射。

如果您执行以下操作,可能会更简单:

  • 在数据库上设置侦听器,并在发生数据库更新时发出 DataValue,而不会出错。

  • 收到错误后,您可以发出带有错误的 DataWrapper

例如:

private final AtomicBoolean isLoading = new AtomicBoolean(false);
private final MediatorLiveData<DataWrapper<List<Card>>> mediatorLiveData = new MediatorLiveData<>();
private MyRepository() {
// requires a cardsDao
// listen for DB changes and emit on callback
mediatorLiveData.addSource(cardsDao.getCards(), data -> mediatorLiveData.setValue(new DataWrapper<>(data, null)));
}
public LiveData<DataWrapper<List<Card>>> cardsData() {
return mediatorLiveData;
}
public void loadCards() {
if (!isLoading.get()) {
isLoading.set(true);
webService.getCards(result -> {
CardResponse res = (CardResponse) result;
List<Card> cardList = res.getCardsList();
// Trigger update in db
getInsertAsyncTask.execute(cardList.toArray(new Card[cardList.size()]));
isLoading.set(false);
}, error -> {
// Emit the error
mediatorLiveData.setValue(new DataWrapper<>(null, error.getMessage()));
isLoading.set(false);
}, TAG);
}
}

最新更新