我想结合2个observable并执行" combine function"在AndroidSchedules.mainThread()上。我添加了.observeOn(AndroidSchedules.mainThread()),但我仍然得到" java.lang.IllegalStateException:不在主线程上#34;。
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 :(得分:5)
在这种情况下,您必须将两个值配对并将其发送到主线程。
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());