等待位置,然后用 rxjava 执行改造调用
Wait for location and then execute retrofit call with rxjava
在我的应用程序中,我需要等待用户位置然后执行改造(当收到位置时)。
我有可观察的工作
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(location -> {
Log.d(TAG, "COORDS: " + location.getLatitude() + ", " + location.getLongitude());
}, e -> Log.e(TAG, e.getMessage()));
但是现在我需要用改造调用调用第二个可观察对象,有没有比将第二个可观察对象嵌套在第一个可观察对象的 onNext() 中更好的方法?
谢谢
是的,您可以使用 flatmap
运算符:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> retrofitApi.getData(location))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)
订阅现在将获得改造调用的结果
如果您需要 return 改造结果和位置,那么您可以使用 zip 运算符:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> Observable.zip(
retrofitApi.getData(location),
Observable.just(location),
(Func2<Response<Data>, Location, ResponseLocation>) (response, location) -> {
return new ResponseLocation(response, location)
}
))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)
其中 ResponseLocation 只是一个 class,它需要一个位置和一个改造结果。然后订阅将获得一个 ResponseLocation 作为它的参数。
编辑
要在调用 Retrofit 之前使用位置,只需展开 lambda:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> {
updateMap(location);
return retrofitApi.getData(location);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)
在我的应用程序中,我需要等待用户位置然后执行改造(当收到位置时)。
我有可观察的工作
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(location -> {
Log.d(TAG, "COORDS: " + location.getLatitude() + ", " + location.getLongitude());
}, e -> Log.e(TAG, e.getMessage()));
但是现在我需要用改造调用调用第二个可观察对象,有没有比将第二个可观察对象嵌套在第一个可观察对象的 onNext() 中更好的方法?
谢谢
是的,您可以使用 flatmap
运算符:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> retrofitApi.getData(location))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)
订阅现在将获得改造调用的结果
如果您需要 return 改造结果和位置,那么您可以使用 zip 运算符:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> Observable.zip(
retrofitApi.getData(location),
Observable.just(location),
(Func2<Response<Data>, Location, ResponseLocation>) (response, location) -> {
return new ResponseLocation(response, location)
}
))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)
其中 ResponseLocation 只是一个 class,它需要一个位置和一个改造结果。然后订阅将获得一个 ResponseLocation 作为它的参数。
编辑
要在调用 Retrofit 之前使用位置,只需展开 lambda:
mlocationService.getLocation()
.timeout(LOCATION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.flatmap(location -> {
updateMap(location);
return retrofitApi.getData(location);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...)