我的演示者如下所示:
// I'm retaining the presenter in a singleton instances map and reuse them
// because they are loading data from the internet and this should be done once
// per app start only
public static ArticlePresenter get(Article article)
{
if (INSTANCES.containsKey(article.id()))
return INSTANCES.get(article.id());
ArticlePresenter instance = new ArticlePresenter();
INSTANCES.put(article.id(), instance);
return instance;
}
@Override
protected void bindIntents()
{
ArrayList<Observable<ArticlePartialStateChanges>> observables = new ArrayList<>();
observables.add(intent(ArticleView::loadArticleIntent)
.doOnNext(article -> L.d("intent: loadArticleIntent"))
.flatMap(article -> AndroInteractor.loadArticle(article)
.map(data -> (ArticlePartialStateChanges) new ArticlePartialStateChanges.Loaded(data))
.startWith(new ArticlePartialStateChanges.LoadingArticle(article))
.onErrorReturn(ArticlePartialStateChanges.LoadingArticleError::new)
.subscribeOn(Schedulers.io())
)
);
Observable<ArticlePartialStateChanges> allIntents = Observable.merge(observables);
ArticleViewState initialState = ArticleViewState.builder().build();
Observable<ArticleViewState> stateObservable = allIntents
.scan(initialState, this::viewStateReducer)
.observeOn(AndroidSchedulers.mainThread());
subscribeViewState(stateObservable, ArticleView::render);
}
我的片段loadArticleIntent
如下所示:
@Override
public Observable<Article> loadArticleIntent()
{
return Observable.just(article).doOnComplete(() -> L.d("Article loaded"));
}
结果
如果片段是第一次创建,我会得到以下 3 项:
- 最初的事件
- 加载文章事件
- 加载的文章或错误事件
如果再次创建片段,它将从地图中检索已经存在的演示者,并重用其中的最后一个已知状态。然后我得到以下信息:
- 最后加载的事件
- 最初的事件
- 加载文章事件
- 加载的文章或错误事件
这并不完美,我需要将逻辑更改为仅发出最后一个已知状态(与屏幕旋转后发生的行为相同)。
我该怎么解决这个问题?