片段方法在每个演示者调用上重复
Fragment methods duplicating on each presenter call
我在开发Andoid天气应用时遇到了问题。在抽屉里我有几个城市,当我 select 在成功请求调用视图方法 updateWeather 之后我 select 那些 Presenter 方法之一,如果错误 - 调用 showError。但问题是,当我得到一个城市(一个片段)的天气,然后从抽屉中选择另一个片段(另一个城市)时,视图方法 updateWeather 或 showError 调用了很多次(准确地说 - 与我切换的次数一样多)片段)。
在 onActivityCReated 中调用方法 presenter.getWeather()
演示者有此代码
void getWeather() {
Log.i("WeatherPresenter", "StartLoading");
getViewState().showLoading();
dataSubscription = repository.getWeatherData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(next -> {
Log.i("LoadWeather", "Success");
getViewState().hideLoading();
getViewState().updateWeatherCurrent(next.getWeatherResponse());
getViewState().updateWeatherHourly(next.getWeatherForecastHourlyResponse());
getViewState().updateWeatherDaily(next.getWeatherForecastDailyResponse());
getViewState().updateLastUpdateTime(dateFormat.format(new Date(next.getUpdatedTime())));
},
error -> {
getViewState().hideLoading();
Log.e("Error", error.getMessage());
getViewState().showError();
});
}
如果发生错误方法 getViewState().showError(),从日志中获取;只调用一次。但是在片段中它调用的次数与我切换片段的次数一样多。
这是日志 - 理解我的意思
08-08 16:35:42.738 31682-31682/dvinc.yamblzhomeproject E/WeatherFragment: Error Shown
08-08 16:35:42.913 31682-31682/dvinc.yamblzhomeproject E/Error: Unable to resolve host "api.openweathermap.org": No address associated with hostname
08-08 16:35:42.914 31682-31682/dvinc.yamblzhomeproject E/WeatherFragment: Error Shown
在演示者中我取消订阅请求
@Override
public void detachView(WeatherView view) {
super.detachView(view);
Log.i("WeatherPresenter", "DetachView");
if (dataSubscription != null) {
dataSubscription.dispose();
}
}
这就是我切换片段的方式
@Override
public void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.commit();
}
以下是我如何从主要演示者(负责导航)调用此方法
void openWeatherFragment(CityEntity cityEntity) {
menuActiveCity = menuRepository.setActiveCity(cityEntity)
.subscribeOn(Schedulers.io())
.subscribe();
getViewState().showFragment(WeatherFragment.newInstanse(cityEntity.getCityTitle()));
}
已解决。问题是因为我用单例注释标记了我所有的演示者,但忘记了 Moxy 组织它的方式是自己注入演示者。
我在开发Andoid天气应用时遇到了问题。在抽屉里我有几个城市,当我 select 在成功请求调用视图方法 updateWeather 之后我 select 那些 Presenter 方法之一,如果错误 - 调用 showError。但问题是,当我得到一个城市(一个片段)的天气,然后从抽屉中选择另一个片段(另一个城市)时,视图方法 updateWeather 或 showError 调用了很多次(准确地说 - 与我切换的次数一样多)片段)。
在 onActivityCReated 中调用方法 presenter.getWeather()
演示者有此代码
void getWeather() {
Log.i("WeatherPresenter", "StartLoading");
getViewState().showLoading();
dataSubscription = repository.getWeatherData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(next -> {
Log.i("LoadWeather", "Success");
getViewState().hideLoading();
getViewState().updateWeatherCurrent(next.getWeatherResponse());
getViewState().updateWeatherHourly(next.getWeatherForecastHourlyResponse());
getViewState().updateWeatherDaily(next.getWeatherForecastDailyResponse());
getViewState().updateLastUpdateTime(dateFormat.format(new Date(next.getUpdatedTime())));
},
error -> {
getViewState().hideLoading();
Log.e("Error", error.getMessage());
getViewState().showError();
});
}
如果发生错误方法 getViewState().showError(),从日志中获取;只调用一次。但是在片段中它调用的次数与我切换片段的次数一样多。
这是日志 - 理解我的意思
08-08 16:35:42.738 31682-31682/dvinc.yamblzhomeproject E/WeatherFragment: Error Shown
08-08 16:35:42.913 31682-31682/dvinc.yamblzhomeproject E/Error: Unable to resolve host "api.openweathermap.org": No address associated with hostname
08-08 16:35:42.914 31682-31682/dvinc.yamblzhomeproject E/WeatherFragment: Error Shown
在演示者中我取消订阅请求
@Override
public void detachView(WeatherView view) {
super.detachView(view);
Log.i("WeatherPresenter", "DetachView");
if (dataSubscription != null) {
dataSubscription.dispose();
}
}
这就是我切换片段的方式
@Override
public void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.commit();
}
以下是我如何从主要演示者(负责导航)调用此方法
void openWeatherFragment(CityEntity cityEntity) {
menuActiveCity = menuRepository.setActiveCity(cityEntity)
.subscribeOn(Schedulers.io())
.subscribe();
getViewState().showFragment(WeatherFragment.newInstanse(cityEntity.getCityTitle()));
}
已解决。问题是因为我用单例注释标记了我所有的演示者,但忘记了 Moxy 组织它的方式是自己注入演示者。