并行处理完成后刷新UI

时间:2016-05-25 15:21:45

标签: android rx-android

我尝试在我的应用中使用RxAndroid并且我已经在这个问题上停留了一段时间。

我想用webservice调用的结果更新每个列表对象,当为每个列表对象完成此处理时,我想刷新一个recyclerview。

OLSLog.v("refreshDistancesFromUser");
Observable.from(mListInterventions).map(this::updateDistanceFromUser).subscribe(new Observer<Subscription>() {
                            @Override
                            public void onCompleted() {
                                OLSLog.v("onCompleted");
                                // refresh recyclerview
                            }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(Subscription pSubscription) {

                        }
                    });

private Subscription updateDistanceFromUser(GMIntervention pIntervention) {
        return WSFacade.getRoutes(mUserLocation.getLatitude(), mUserLocation.getLongitude(), pIntervention.getSite()
            .getLatitude(), pIntervention.getSite().getLongitude()).observeOn(Schedulers.computation()).subscribe
                (pResponseDto -> {
            Long vDistance = pResponseDto.getListRoutes().get(0).getLegList().get(0).getDistance().getValue();
            Long vDuration = pResponseDto.getListRoutes().get(0).getLegList().get(0).getDuration().getValue();
            OLSLog.d("distance from user: " + vDistance);
            pIntervention.setDistanceFromUser(new GMDistanceFrom(vDistance, vDuration));
        }, pThrowable -> OLSLog.e("fail to get directions", pThrowable));
    }

我的问题是onCompleted()在启动最后一个列表对象的处理时调用,而不是在所有处理完成时调用。

05-25 16:44:00.254 V/### HomeActivity: refreshDistancesFromUser
05-25 16:44:00.282 D/OkHttp: --> GET https://maps.googleapis.com/maps/api/directions/json http/1.1
05-25 16:44:00.288 V/### HomeActivity$1: onCompleted
05-25 16:44:00.296 D/OkHttp: --> GET https://maps.googleapis.com/maps/api/directions/json? http/1.1
05-25 16:44:00.301 D/OkHttp: --> GET https://maps.googleapis.com/maps/api/directions/json http/1.1
05-25 16:44:00.969 D/OkHttp: <-- 200  https://maps.googleapis.com/maps/api/directions/json (668ms, unknown-length body)
05-25 16:44:00.977 D/OkHttp: <-- 200  https://maps.googleapis.com/maps/api/directions/json (680ms, unknown-length body)
05-25 16:44:00.981 D/OkHttp: <-- 200  https://maps.googleapis.com/maps/api/directions/json (698ms, unknown-length body)
05-25 16:44:00.990 D/### HomeActivity: distance from user: 7320
05-25 16:44:00.991 D/### HomeActivity: distance from user: 17387
05-25 16:44:00.992 D/### HomeActivity: distance from user: 2753

我怎样才能等待&#34;所有updateDistanceFromUser()处理确实已经完成了#34;刷新我的Recyclerview?

感谢。

2 个答案:

答案 0 :(得分:0)

问题在于您将输入映射到即时创建的订阅,然后逐个返回。 您可以组合这些可观察对象,以便在同一个流中接收结果。

可以帮助您的一位运营商是Merge

答案 1 :(得分:0)

你不应该在地图或类似的操作符内订阅,这会破坏目的或RxJava。

我会使用flatMap将您的Observable<GMIntervention>变成Observable<ResponseDto>。如果subscribeOn(Schedulers.io())是同步的,则可能需要observeOn(AndroidSchedulers.mainThread())WSFacade.getRoutes

如果您想要正确执行所有操作,请仅将结果发布到Subscriber方法内的UI。那会更复杂一点。

修改:如果您是新手,请查看Grokking RxJava