我执行了一个 Retrofit2 observable 调用,在完成后它立即链接到另一个 observable 以将结果存储到数据库中。它看起来很简单,如下所示:
protected Observable<List<Long>> buildUseCaseObservable() {
return mDataRepo.fetchCountries().flatMap(new Function<List<CountryModel>, ObservableSource<List<Long>>>() {
@Override
public ObservableSource<List<Long>> apply(@NonNull List<CountryModel> countryModels) throws Exception {
return mDataRepo.storeCountries(countryModels);
}
});
}
现在我的问题是我希望订阅者取回第一个可观察的结果。所以我喜欢
订户取回<List<CountryModel>>
而不是现在得到的<List<Long>>
。有办法做到这一点吗?不确定 concat 是否有帮助?
事实上,是的,你可以使用flatMap()
变体与resultSelector
,您可以从输入和输出中选择或组合flatMap()
在您的情况下,只需返回获取的国家/地区而不是 ID:
protected Observable<List<CountryModel>> buildUseCaseObservable() {
Repo mDataRepo = new Repo();
return mDataRepo.fetchCountries()
.flatMap(new Function<List<CountryModel>, ObservableSource<List<Long>>>() {
@Override
public ObservableSource<List<Long>> apply(
@android.support.annotation.NonNull List<CountryModel> countryModels) throws Exception {
return mDataRepo.storeCountries(countryModels);
}
}, new BiFunction<List<CountryModel>, List<Long>, List<CountryModel>>() {
@Override
public List<CountryModel> apply(List<CountryModel> countryModels,
List<Long> longs) throws Exception {
return countryModels;
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)