gpt4 book ai didi

java - Rxjava - 当链接 observables 时如何取回其他类型的流(返回值)而不是当前?

转载 作者:行者123 更新时间:2023-12-05 00:51:17 25 4
gpt4 key购买 nike

我有一个 retrofit2 observable 调用,我执行并在它完成后立即链接到另一个 observable 以将结果存储到 db 中。它看起来很简单:

    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);
}
});
}

现在我的问题是我希望订阅者取回第一个 observable 的结果。所以我喜欢
订阅者取回 <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;
}
});
}

关于java - Rxjava - 当链接 observables 时如何取回其他类型的流(返回值)而不是当前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44622698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com