我嘗試在我的應用程序中使用RxAndroid,並在此問題上停留了一段時間。並行處理完成後刷新UI
我想更新每個列表對象與Web服務調用的結果,並且當這個處理完成每個,我想刷新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
如何「等待」所有updateDistanceFromUser()處理真的「完成」刷新我的recyclerview?
謝謝。
什麼是updateDistanceFromUser?你的服務電話? – DariusL
是的。我在之前的文章中有過。 – Narayane
你是否閱讀過'mListInterventions'的任何內容?因爲它看起來像你只是寫信給它。 – DariusL