我需要对服务器执行几个并行请求以检索数据。如何使用可观察对象做到这一点。
我的实施

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显示警告。
android - Android Observables.zip列出observables-LMLPHP

最佳答案

zip的解决方案很丑陋且冗长,因为您有多达10个可观察值,每种类型都不同。另外,您需要将每个结果转换为正确的类型,然后再将其存储到数据库中。

由于您的目标是获取和存储各种数据(如我所见),因此我将每个任务表示为一个单独的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
                }
            });


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

10-04 22:15
查看更多