我有一个同步可观测值和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();

最佳答案

正如我在评论中提到的那样:您可以将zip分为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字段的某些数据类。

关于java - 如何创建2个zip并合并可观察对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56728161/

10-10 05:32