RxJava中的Merge运算符是否可以并行运行api调用?

时间:2018-07-28 14:44:55

标签: android rx-java observable retrofit2

我正在将rxjava用于翻新

我正在查看RxJava运算符MERGE


我已经实现了合并操作符,如下所示,我将4个api调用合并为一个,如下所示

private void inititiate() {

        List<Observable<?>> observables = Lists.newArrayList(categoriesObservable, walletObservable, settingsObservable, ratingsObservable);
        Observable.merge(observables).toList().subscribe(new Consumer<List<Object>>() {
            @Override
            public void accept(List<Object> objects) throws Exception {

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }



    Observable<CategoryHomeModel[]> categoriesObservable = retrofit
            .create(Restapi.class)
            .getCategories(new GetHeaders().prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<WalletBalance> walletObservable = retrofit
            .create(Restapi.class)
            .getWalletBalance(new GetHeaders().prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<Settings> settingsObservable = retrofit
            .create(Restapi.class)
            .getSettings(new GetHeaders().prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

    Observable<SwapSettings> ratingsObservable = retrofit
            .create(Restapi.class)
            .rateMySwap(new GetHeaders().prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

这样做是不是所有4个api调用都并行运行,还是仅一个接一个地执行?

例如:如果每个呼叫需要1秒才能完成,那么通过上述操作我可以在1秒内而不是4秒内完成所有四个呼叫


0 个答案:

没有答案