Mosby MVI - 如何重用 Presenter
Mosby MVI - How to reuse presenter
我的演示者如下所示:
// 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 项:
- 初始事件
- 加载文章事件
- 加载的文章或错误事件
如果再次创建该片段,它将从地图中检索已经存在的展示者,并重新使用其中的最后一个已知状态。然后我得到以下信息:
- 最后加载的事件
- 初始事件
- 加载文章事件
- 加载的文章或错误事件
这并不完美,我需要更改逻辑以仅发出最后已知的状态(与屏幕旋转后发生的行为相同)。
我该如何解决这个问题?
不要重复使用 Presenter。这不是它想要的方式。重用它们在今天可能会奏效,但不能保证它在未来也能奏效。
所以基本上您只想使用从业务逻辑中检索到的数据,对吗?基本上,您希望这部分预加载数据 AndroInteractor.loadArticle(article)
。所以只需在应用程序启动时调用它,而不是整个演示者。也许你使用了一些内存/磁盘缓存库,或者只是在 AndroInteractor.loadArticle(article)
内部使用了一个 BehaviorSubject
。那一个保存最新数据(如果有的话)。
所以你的问题只是 "business logic problem" / "caching of data problem" 而不是真正的 "Presenter" 问题。因此,你应该在业务逻辑层解决这个问题,即AndroInteractor.loadArticle(article)
而不是保留整个Presenter。
我的演示者如下所示:
// 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 项:
- 初始事件
- 加载文章事件
- 加载的文章或错误事件
如果再次创建该片段,它将从地图中检索已经存在的展示者,并重新使用其中的最后一个已知状态。然后我得到以下信息:
- 最后加载的事件
- 初始事件
- 加载文章事件
- 加载的文章或错误事件
这并不完美,我需要更改逻辑以仅发出最后已知的状态(与屏幕旋转后发生的行为相同)。
我该如何解决这个问题?
不要重复使用 Presenter。这不是它想要的方式。重用它们在今天可能会奏效,但不能保证它在未来也能奏效。
所以基本上您只想使用从业务逻辑中检索到的数据,对吗?基本上,您希望这部分预加载数据 AndroInteractor.loadArticle(article)
。所以只需在应用程序启动时调用它,而不是整个演示者。也许你使用了一些内存/磁盘缓存库,或者只是在 AndroInteractor.loadArticle(article)
内部使用了一个 BehaviorSubject
。那一个保存最新数据(如果有的话)。
所以你的问题只是 "business logic problem" / "caching of data problem" 而不是真正的 "Presenter" 问题。因此,你应该在业务逻辑层解决这个问题,即AndroInteractor.loadArticle(article)
而不是保留整个Presenter。