j2emanue
j2emanue

Reputation: 62549

Rxjava - when chaining observables how to get back other type of stream(return value) instead of current?

I have a retrofit2 observable call i execute and right after it completes it chains to another observable to store the results into a db.It looks simply like this:

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

Now my issue is i want the subscriber to get back the results of the first observable. So id like the subscriber to get back <List<CountryModel>> instead of what its getting back now of <List<Long>>. Is there anyway to do this ? Not sure if concat can help ?

Upvotes: 4

Views: 1284

Answers (1)

yosriz
yosriz

Reputation: 10267

Actually, yes, you can use flatMap() variant with resultSelector, their you can select or combine from both input and output of flatMap() and in your case simply returns the fetched countries instead of ids:

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

Upvotes: 5

Related Questions