I'm using the new Mosby MVI library for a new demo app. When defining intents in a presenter it is inconsistent when the intent is triggered/emitted when the view is attached.
For example: Let's define very simple intent in an activity
public Observable<Boolean> intentLoadData(){
return Observable.just(true);
}
The presenter binds the intent like:
@Override
protected void bindIntents() {
Observable<MailListViewState> loadData = intent(ExampleViewContract::intentLoadData).flatMap(interactor::loadData)
.observeOn(AndroidSchedulers.mainThread());
subscribeViewState(loadData, ExampleViewContract::render);
}
This intent works just fine. When navigating to a different activity (detail view) and navigating back, bindIntents()
is called the the intent is recreated. intentLoadData()
doesn't emit a new item and the MviBasePresenter will provide the previous ViewState using the internal BehaviorSubject.
My problem is: When I slightly adjust the Intent (for reloading the data). The intent starts to emit an item when the View is reattached.
So lets change the intent to:
private PublishSubject<Boolean> mReloadDataSubject = PublishSubject.create();
private void reloadData(){
mReloadDataSubject.onNext(true);
}
public Observable<Boolean> intentLoadData(){
return mReloadDataSubject.startWith(true);
}
No when navigating to a new activity and back. The intent emits a new item when the view is reattached. In my case this results in a new APU call to the backend to reload the data, rather than reusing the last ViewState. This happens even when reloadData()
is never called.
This behavior feels very inconsistent. How can I feel more in control when an intent is triggered during reattaching the view?
Update: For me even more interesting is, how do I avoid the automatic emitting of the intents when reattaching, without completing the Observable. With the introduction of a PublishSubject, the activity will reload the entire data even when just rotating.