gpt4 book ai didi

lambda - 将两个 Observables 的平面图的 lambda 更改为两个 Singles 的平面图时出错

转载 作者:行者123 更新时间:2023-12-02 13:14:21 25 4
gpt4 key购买 nike

我想合并两个 API 请求的结果。最初我让它们返回 Observable,但为了正确执行它,我猜响应应该是 Single。

这是我最初的改造接口(interface):

interface RemoteGeocodingService {

@GET("json")
fun requestCityAddressByName(
@Query("address") address: String
): Observable<LocationResponse>
}

interface RemoteWeatherService {

@GET("{latitude},{longitude}")
fun requestWeatherForCity(
@Path("latitude") latitude: String,
@Path("longitude") longitude: String
): Observable<WeatherResponse>
}

我使用 lambda 像这样组合它们的结果:
    override fun getWeather(cityName: String): Observable<WeatherDetailsDTO>? {
return remoteWeatherDataSource.requestCityAddressByName(cityName)
.flatMap({ responseFromServiceA -> remoteWeatherDataSource.requestWeatherForCity(responseFromServiceA.results[0].geometry.location.lat.toString(), responseFromServiceA.results[0].geometry.location.lng.toString()) },
{ responseFromServiceA, responseFromServiceB ->
TransformersDTO.transformToWeatherDetailsDTO(responseFromServiceA.results[0].formatted_address, responseFromServiceB)
})
.retry()
}

但是,当我将每个 API 接口(interface)方法的请求结果更改为使用 Single 而不是 Observable 时,我无法执行此操作 - 我在 Android Studio IDE 编辑器中出现错误。这是我尝试过的:
  override fun getWeather(cityName: String): Single<WeatherDetailsDTO> {
return remoteWeatherDataSource.requestCityAddressByName(cityName)
.flatMap({ responseFromServiceA: LocationResponse -> remoteWeatherDataSource.requestWeatherForCity(responseFromServiceA.results[0].geometry.location.lat.toString(), responseFromServiceA.results[0].geometry.location.lng.toString()) },
{ responseFromServiceA: LocationResponse, responseFromServiceB: WeatherResponse ->
TransformersDTO.transformToWeatherDetailsDTO(responseFromServiceA.results[0].formatted_address, responseFromServiceB)
})
.retry()

IDE 标记为错误 .flatMap 运算符并说:

None of the following functions can be called with the arguments supplied. flatMap(((t: LocationResponse) → SingleSource!)!)   where R cannot be inferred for   fun flatMap(mapper: ((t: LocationResponse) → SingleSource!)!): Single! defined in io.reactivex.Single flatMap(Function!>!)   where R cannot be inferred for   fun flatMap(mapper: Function!>!): Single! defined in io.reactivex.Single



如何解决?

最佳答案

没有Single.flatMap(Function, BiFunction)方法。您必须映射到内部结果才能使项目配对:

remoteWeatherDataSource.requestCityAddressByName(cityName)
.flatMap({ a: LocationResponse ->
remoteWeatherDataSource.requestWeatherForCity(
a.results[0].geometry.location.lat.toString(),
a.results[0].geometry.location.lng.toString()
)
.map { b: WeatherResponse ->
TransformersDTO.transformToWeatherDetailsDTO(
a.results[0].formatted_address,
b
)
}
})

关于lambda - 将两个 Observables 的平面图的 lambda 更改为两个 Singles 的平面图时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48250671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com