合并Flowable <a> with a Single<b>

时间:2018-04-02 09:04:06

标签: java reactive-programming rx-java2

I have two different documents with a relation:

class Foo {
    String getBarId();
    String message();
}
class Bar {
    String id();
    String description();
}

Given an Flowable of Foo I have to merge every item emitted with a Single of Bar. Imagine in non Reactive way the following:

getStreamOfFoo().forEach(f -> {
           Bar b = getBarById(f.getBarId());
           sendMessage(f.getMessage(), bar.getDescription());
});

So I have the method getStreamOfFoo that returns a Flowable<Foo>, the getBarById the returns a Single<Bar> and the sendMessage that returns a Completable. This is my code, there is a compiling error that I don't know how to manage:

getFoo().flatMapCompletable(f ->
        Single.zip(Single.just(f), getBarById(f.getBarId()),
        (foo,bar) -> sendMessage(foo,bar)));

Thanks for the help.

1 个答案:

答案 0 :(得分:0)

好的,解决了,我接近解决方案。这是代码:

Completable sendMessage() {
     getFoo().map(f -> 
         Single.zip(
            Single.just(f),
            getBarById(f.getBarId()),
            (foo,bar) -> sendMessage(foo, bar)))
     .flatMapCompletable(identity());
}