我使用LiveData + MVP架构。
我在onCreate中的观点对Presenter说:
presenter = MyApplication.me().getAppComponent().getBasketPresenter();
presenter.attachView(this);
presenter.viewIsReady();
我的演示者从Room DB获得LiveData
:
LiveData<List<BasketItem>> listLiveData = MyApplication.me().getDatabase().basketDao().getAll();
之后我需要观察这个LiveData:
listLiveData.observe(getView(), basketItems -> {
callView(view -> {
view.setData(basketItems);
});
});
在观察回调中,我可以在callView方法中检查view
:
void callView(ViewAction<T> action) {
if (getView() == null) return;
action.call(view);
}
但listLiveData.observe(getView()
呢?我怎么检查呢?我需要写:
if (getView() == null) return;?
一般来说我做的还是正确的?我在演示者中获得了LiveData,并观察了视图?