如何在observable内部使用sharedPreferences,因为我知道在observable之外获取价值是一个坏习惯。
我有这样的东西:
class City {
private String tempCelsius;
private String tempKelvin;
// other fields
public getTempCelsius() {
return tempCelsius;
}
public getTempKelvin() {
return tempKelvin;
}
}
在主持人中,我有:
public Single<City> getData() {
return dataManager.loadCitiesFromDb().
map(city -> dataManager.makeApiCall(city)
//getting data, error handling
)
.subscribe(city -> view.showCityData(city));
}
通过我的DataManager包含PreferenceHelper实例的方式。
如何基于SharedPreferences中的值来showCityData
使用温度。
如果Kelvin
使用City的方法getTempKelvin()
,否则,如果Celsius
使用City的方法getTempCelsius()
。
我从Api收到了两个消息,但根据保存的首选项只需要显示一次。