Android Observables.zip列出了observables

时间:2017-09-04 12:07:51

标签: android rx-java observable

我需要向服务器执行几个并行请求以检索数据。我怎样才能使用observables。 我的实施

public void get(String s, Observer<Boolean> observer) {
    APIService apiService = App.getApi();
    final Observable<User> oUser = apiService.getUser(Integer.valueOf(s));
    final Observable<List<DocType>> oDocType = apiService.getDocType();
    final Observable<List<TaskTypes>> oTaskTypes = apiService.getTaskType();
    final Observable<List<DocTaskLeave>> oTaskLeave = apiService.getTaskLeave();
    final Observable<List<DocStatus>> oDocStatus = apiService.getDocStatus();
    final Observable<List<DocPayment>> oDocPayment = apiService.getPaymentType();
    final Observable<List<DocCredit>> oDocCredit = apiService.getPaymentCredit();
    final Observable<List<ClientRegion>> oClientRegion = apiService.getClientRegion();
    final Observable<List<ProductRules>> oProductRules = apiService.getProductsRules();
    final Observable<List<ReportTypes>> oReportTypes = apiService.getReportType();
    List<Observable> observables = new ArrayList<>();
    observables.add(oUser);
    observables.add(oDocType);
    observables.add(oTaskTypes);
    observables.add(oTaskLeave);
    observables.add(oDocStatus);
    observables.add(oDocPayment);
    observables.add(oDocCredit);
    observables.add(oClientRegion);
    observables.add(oProductRules);
    observables.add(oReportTypes);
    Observable obs = Observable.zip(observables, new FuncN() {
        @Override
        public Object call(Object... args) {
            return null;
        }
    });
    obs.subscribe(observer);
}

我认为这是错误的,因为。 IDE显示警告。 enter image description here

1 个答案:

答案 0 :(得分:1)

使用zip的解决方案将是丑陋且冗长的,因为您有多达10个不同类型的可观察对象。此外,在将每个结果存储到DB中之前,您需要将每个结果转换为正确的类型。

由于你的目标是获取和存储各种数据(我可以看到),我会将每个任务表示为一个单独的Completable,它自己执行Scheduler

final Completable oUser = apiService.getUser(Integer.valueOf(s))
        .doOnNext(result -> /* save to db */) // or flatMap
        .toCompletable()
        .retry(3) // in case of upstream error - retries this task (up to 3 times)
        .onErrorComplete() // replaces upstream errors with onCompleted event
        .subscribeOn(Schedulers.io());

final Completable oDocType = apiService.getDocType()
        .doOnNext(result -> /* save to db */)
        .toCompletable()
        .retry(3)
        .onErrorComplete()
        .subscribeOn(Schedulers.io());

// ... etc

Completable.merge(oUser, oDocType, oTaskTypes, oTaskLeave, oDocStatus, 
        oDocPayment, oDocCredit, oClientRegion, oProductRules, oReportTypes)
            .subscribe(new CompletableSubscriber() {
                @Override
                public void onCompleted() {
                    // all done!
                }

                @Override
                public void onError(Throwable e) {
                    // handle error
                }

                @Override
                public void onSubscribe(Subscription d) {
                    // manage subscription if needed
                }
            });

所有任务并行执行。此外,您可以为每个请求或任何其他附加逻辑设置重试计数。