These are chat archives for ReactiveX/RxJava

23rd
May 2018
Marc Guilera
@marcguilera
May 23 2018 13:19

hi i need help with something. i have 3 Singles I need to combine.

This is the not functioning pseudocode:

 Single<List<FilmModel>> filmModels = filmModelFactory.getMany(1,2);
        // The next two lines should be for each model in the list
        Single<FilmTypeModel> filmTypeModel = filmTypeModelFactory.getById(filmModel.getTypeId());
        Single<FilmTypeBonusModel> filmTypeBonusModel = filmTypeBonusModelFactory.getByFilmId(filmModel.getId());
        Single<BonusModel> bonusModel = bonusModelFactory.getById(filmTypeBonusModel.getId());
        // Use the results from all the singles to create an output object
        return new FilmEntityImpl(filmModel, filmTypeModel, bonusModel); // should be a single of a Liast of film entities

thanks!

Ignacio Baca Moreno-Torres
@ibaca
May 23 2018 13:23
Use the zip operator to combine them all…
Single.zip(Single.just(1), Single.just("a"), Single.just(Boolean.TRUE), (num, str, bool) -> new Object() {
    int theNum = num; String theStr = str; boolean theBool = bool;
}).subscribe(n -> System.out.println(n.theNum + ":" + n.theStr + ":" + n.theBool));
Marc Guilera
@marcguilera
May 23 2018 13:26
thats exactly it
thanks
wait but they are dependent on each other
like the second single is get(first.id())
how can i do that?
Ignacio Baca Moreno-Torres
@ibaca
May 23 2018 13:30
uhm so you need to do it sequentially, then just use nested flatMaps and return the final model in the last one...
just(1).flatMap(num -> just(“a”).flatMap(str -> just(TRUE).map(bool -> new CombineThemAll(num, str, bool))))
Marc Guilera
@marcguilera
May 23 2018 13:46
@ibaca this is the closest I got but I don't like having to block there.
@Override
    public Single<ImmutableCollection<FilmEntity>> getCollection(Pagination pagination) {
        checkNotNull(pagination);
        return filmModelFactory
                .getMany(pagination.getLimit(),pagination.getOffset())
                .map(this::getFilmEntities);
    }

    private ImmutableCollection<FilmEntity> getFilmEntities(ImmutableCollection<FilmModel> filmModels) {
        return filmModels
                .stream()
                .map(this::getFilmEntity)
                .collect(toImmutableCollection());
    }

    private FilmEntity getFilmEntity(FilmModel filmModel) {
        FilmTypeModel type = getFilmTypeModel(filmModel.getId()).blockingGet();
        BonusModel bonus = getBonusModel(filmModel.getId()).blockingGet();
        return new FilmEntityImpl(filmModel, type, bonus);
    }

    private Single<FilmTypeModel> getFilmTypeModel(long filmId) {
        return filmTypeModelFactory.getById(filmId);
    }

    private Single<BonusModel> getBonusModel(long filmId) {
        return filmTypeBonusModelFactory
                .getByFilmId(filmId)
                .flatMap(filmTypeBonusModel -> bonusModelFactory.getById(filmTypeBonusModel.getId()));
    }
also getFilmTypeModel(filmModel.getId()) could potentially be Single.error and I'd like to propagate that error in the public return
Ignacio Baca Moreno-Torres
@ibaca
May 23 2018 14:03
public Single<List<FilmEntity>> getCollection(Pagination pagination) {
    return filmModelFactory
            .getMany(pagination.getLimit(),pagination.getOffset())
            .flatMap(this::getFilmEntities);
}
private Single<List<FilmEntity>> getFilmEntities(Collection<FilmModel> filmModels) {
    return Observable.fromIterable(filmModels)
            .flatMapSingle(this::getFilmEntity)
            .toList();
}
private Single<FilmEntity> getFilmEntity(FilmModel filmModel) {
    return Single.zip(
            getFilmTypeModel(filmModel.getId()),
            getBonusModel(filmModel.getId()),
            (type, bonus) -> new FilmEntityImpl(filmModel, type, bonus));
}
private Single<FilmTypeModel> getFilmTypeModel(long filmId) {
    return filmTypeModelFactory.getById(filmId);
}
private Single<BonusModel> getBonusModel(long filmId) {
    return filmTypeBonusModelFactory.getByFilmId(filmId)
            .flatMap(n -> bonusModelFactory.getById(n.getId()));
}
Marc Guilera
@marcguilera
May 23 2018 14:06
@ibaca perfect! many thanks man
Ignacio Baca Moreno-Torres
@ibaca
May 23 2018 14:07
:+1: