如何创建2个zip并合并可观察对象?

时间:2019-06-23 22:30:29

标签: java observable rx-java rx-java2 rx-android

我有一个同步可观测值和10个其他异步可观测值,它们取决于同步可观测值。如何将它们压缩在一起并得到最终结果?邮编仅接受9个参数..如果我必须将其拆分为2个邮编,那么由于我只有1个同步可观察到的东西,因此该怎么做。请帮忙。 下面是代码:这里的帐户是同步的。

Observable aObservable = getAObservable(accountId);      
Account account = aObservable.toBlocking().single();
Observable<List<C>> cObservable = getCObservable(saleInfo);
Observable<B> bObservable = getBObservable(account);
Observable<D> dObservable = getDObservable(account);
Observable<E> eObservable = getEObservable(account);
Observable<F> fObservable = getFObservable(account);
Observable<G> gObservable = getGObservable(account);
Observable<H> hObservable = getHObservable(account);
Observable<I> iObservable = getIObservable(account);
Observable<J> jObservable = getJObservable(account);

Observable<SaleFile> observable =
        Observable.zip(
                cObservable,
                bObservable,
                dObservable,
                eObservable,
                fObservable,
                gObservable,
                hObservable,
                iObservable,
                jObservable,
                (o1, o2, o3, o4, o5, o6, o7, o8, o9) ->
                        new SaleFile()
                                .withA(account)
                                .withB(o1)
                                .withC(o2)
                                .withD(o3)
                                .withE(o4)
                                .withF(o5)
                                .withG(o6)
                                .withH(o7)
                                .withI(o8)
                                .withJ(o9));

return observable.toBlocking().single();

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样:您可以将压缩分为2个可观察对象,然后再次将其压缩。因此,它看起来像:

Observable<TmpObjectForFirstSeven> zip1 = Observable.zip(
    observable1,
    observable2,
    observable3,
    observable4,
    observable5,
    observable6,
    observable7,
    {(o1, o2, o3, o4, o5, o6, o7) -> 
         new TmpObjectForFirstSeven(o1, o2, o3, o4, o5, o6, o7)
    }
)

Observable<TmpObjectForFirstThree> zip2 = Observable.zip(
    observable8,
    observable9,
    observable10,
    {(o8, o9, o10) -> 
         new TmpObjectForSecondThree(o8, o9, o10)
    }
)

Observable<SaleFile> observable = Observable.zip(
    zip1, 
    zip2, 
    {(tmpForSeven, tmpForThree) -> 
        new SaleFile().withA(account)
                      .withB(tmpForSeven.o1())
                      .withC(tmpForSeven.o2())
                      .withD(tmpForSeven.o3())
                      .withE(tmpForSeven.o4())
                      .withF(tmpForSeven.o5())
                      .withG(tmpForSeven.o6())
                      .withH(tmpForSeven.o7())
                      .withI(tmpForThree.o8())
                      .withJ(tmpForThree.o9()))
    }
)

TmpObjectForFirstThreeTmpObjectForFirstSeven是一些只有3和7个字段的数据类。