2016-05-25 60 views
0

我嘗試在我的應用程序中使用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?

謝謝。

+0

什麼是updateDistanceFromUser?你的服務電話? – DariusL

+0

是的。我在之前的文章中有過。 – Narayane

+0

你是否閱讀過'mListInterventions'的任何內容?因爲它看起來像你只是寫信給它。 – DariusL

回答

0

問題是您將輸入映射到即時創建的訂閱,然後逐個返回。 您可以合併這些觀察值,以便在相同的流中接收結果。

一位能夠幫助您的操作員是Merge

+0

不知道該怎麼辦:在我的情況下,合併的Observables不具有相同的(GMIntervention與WSCallResult),在這種情況下的任何示例會不勝感激:) – Narayane

0

您不應該在地圖或類似運營商的內部訂閱,這會破壞目的或RxJava。

我會用flatMap把你的Observable<GMIntervention>變成Observable<ResponseDto>。如果WSFacade.getRoutes是同步的,則可能需要subscribeOn(Schedulers.io())observeOn(AndroidSchedulers.mainThread())

如果您想正確執行所有操作,只需將結果發佈到Subscriber方法中的UI中即可。這會更復雜一點。

編輯:你應該檢查出Grokking RxJava如果你是新手。

+0

我讀過它,我已經嘗試flatMap(),但是當我訂閱Observable 時,是否有可能以及如果我如何訪問相關GMIntervention來更新它? – Narayane

+0

我想做類似的事情,但這是不可能的:Observable.from(mListInterventions).flatMap(vIntervention - > getDistanceFromUser(vIntervention)) .subscribe(pResponseDto - > { Long vDistance = pResponseDto.getListRoutes()。 get(0).getLegList()。get(0).getDistance()。getValue(); Long vDuration = pResponseDto.getListRoutes()。get(0).getLegList()。get(0).getDuration()。 getValue(); vIntervention.setDistanceFromUser(new GMDistanceFrom(vDistance,vDuration)); }); – Narayane