Ilya Mashin
Ilya Mashin

Reputation: 954

Issue with .zip for iterable (rxJava)

I have code that with four Observable. I apply the Observable.zip to them:

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);
                }
            });

But i got error:

enter image description here

The message says that a method with such a signature was not found (maybe i not right), but i go to declaration and see that the signature, sort of like, is suitable.

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

What i does wrong?

P.S: i thing this bug RxJava2Iterableisbroken

Upvotes: 1

Views: 3384

Answers (2)

akarnokd
akarnokd

Reputation: 70007

It's not a bug. You need a Function<? super Object[] because of the generics limitation of Java. This is also explained in 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);
            }
        });

Upvotes: 7

karandeep singh
karandeep singh

Reputation: 2334

Flowable and Single are two different entities in RxJava. Flowable implements Publisher, where as Single implements SingleSource. When you are trying to use zip operator, it expects array list of objects which implements Publisher and hence the error.

Upvotes: 0

Related Questions