.zip用于可迭代(rxJava)的问题

时间:2018-09-30 16:12:16

标签: android rx-java

我有四个可观察的代码。我将Observable.zip应用于他们:

Observable<Currencies> usd = CoinMarket.getMarketApi().getCurrencies();
    Observable<Currencies> rub = CoinMarket.getMarketApi().getCurrencies("RUB");
    Observable<Currencies> eur = CoinMarket.getMarketApi().getCurrencies("EUR");
    Observable<Currencies> btc = CoinMarket.getMarketApi().getCurrencies("BTC");

    List<Observable<Currencies>> singles = new ArrayList<>();
    singles.add(usd);
    singles.add(rub);
    singles.add(eur);
    singles.add(btc);

    Observable<Currencies> c = Observable.zip(singles, new Function<Currencies[], Currencies>() {
                @Override
                public Currencies apply(@NotNull Currencies[] objects) throws Exception {
                    return ramming(objects);
                }
            });

但是我得到了错误:

enter image description here

该消息说没有找到具有这种签名的方法(也许我不对),但是我去声明了一下,发现这种签名是合适的。

 public static <T, R> Observable<R> zip
 (Iterable<? extends ObservableSource<? extends T>> sources,
 Function<? super Object[], ? extends R> zipper)

我做错了什么?

P.S:我认为这个错误RxJava2Iterableisbroken

2 个答案:

答案 0 :(得分:1)

这不是错误。由于Java的通用限制,您需要Function<? super Object[]the Javadocs of zips中也对此进行了解释。

Observable<Currencies> usd = CoinMarket.getMarketApi().getCurrencies();
Observable<Currencies> rub = CoinMarket.getMarketApi().getCurrencies("RUB");
Observable<Currencies> eur = CoinMarket.getMarketApi().getCurrencies("EUR");
Observable<Currencies> btc = CoinMarket.getMarketApi().getCurrencies("BTC");

List<Observable<Currencies>> singles = new ArrayList<>();
singles.add(usd);
singles.add(rub);
singles.add(eur);
singles.add(btc);

Observable<Currencies> c = Observable.zip(singles, new Function<Object[], Currencies>() {
            @Override
            public Currencies apply(@NotNull Object[] objects) throws Exception {
                Currencies[] currencies = new Currencies[objects.length];
                for (int i = 0; i < objects.length; i++) {
                     currencies[i] = (Currencies)objects[i];
                }
                return ramming(currencies);
            }
        });

答案 1 :(得分:0)

Flowable和Single是RxJava中的两个不同实体。 Flowable实现Publisher,而Single实现SingleSource。当您尝试使用zip运算符时,它期望实现Publisher的对象的数组列表,从而导致错误。