2016-05-01 2 views
0

私は2つの観測値を結合し、AndroidSchedules.mainThread()で "combine関数"を実行したいと考えています。 .observeOn(AndroidSchedules.mainThread())を追加しましたが、 "java.lang.IllegalStateException:メインスレッドではありません"というメッセージが表示されます。mainThreadのcombineLatest()関数

Observable<List<Post>> animateCameraAndGetPostsByProjection = Observable.combineLatest(
      mapObservableProvider.getMapReadyObservable(), 
      LocationService.getUpdatedOrLastKnownLocation(this), 
      (googleMap, location) -> { 
       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),15); 
       googleMap.moveCamera(cameraUpdate); 
       return googleMap.getProjection().getVisibleRegion(); 
      }) 
      .flatMap(vr -> new RestService().getApi().getPostsByMapProjection(vr.farLeft.latitude,vr.farLeft.longitude,vr.nearRight.latitude,vr.nearRight.longitude)) 
      .observeOn(AndroidSchedulers.mainThread()); 
+0

どこにエラーがありますか? APIでは、最終結果の受信ではなくメインスレッドで呼び出す必要があります。 – akarnokd

+0

エラーはgoogleMap.moveCamera(cameraUpdate)を指しています。私は、この関数はメインスレッドで実行する必要がありますが、私はどのように組み合わせ関数のスレッドを設定するのか分からないことを知っている。 –

+0

observeOnを両方のソースに置きます。両方とも値がある場合、関数はメインスレッドで実行されます。 – akarnokd

答えて

4

この場合、2つの値をペアにしてメインスレッドに送信する必要があります。

Observable<List<Post>> animateCameraAndGetPostsByProjection = Observable.combineLatest(
    mapObservableProvider.getMapReadyObservable(), 
    LocationService.getUpdatedOrLastKnownLocation(this), 
    (googleMap, location) -> Pair.of(googleMap, location) 
) 
.observeOn(AndroidSchedulers.mainThread()) 
.map(pair -> { 
    CameraUpdate cameraUpdate = CameraUpdateFactory 
     .newLatLngZoom(new LatLng(pair.second.getLatitude(), 
       pair.second.getLongitude()),15); 
    pair.first.moveCamera(cameraUpdate); 
    return googleMap.getProjection().getVisibleRegion();   
}) 
.flatMap(vr -> new RestService().getApi() 
    .getPostsByMapProjection(vr.farLeft.latitude,vr.farLeft.longitude, 
     vr.nearRight.latitude,vr.nearRight.longitude) 
) 
.observeOn(AndroidSchedulers.mainThread()); 
+0

observeOn mainThreadのポイントは何回ですか?どこに登録するの? –

+0

RestServiceが再び非同期になり、バックグラウンドスレッドから放出されることがあります。 observeOnはメインスレッドのデータに反応するようにします。 – akarnokd

+0

combineLatestを変更するには?たとえば、Observablesの1つを削除しますか? –

関連する問題