ReactiveX - 单身 单身

问题描述 投票:0回答:1

我正在寻找一个采用干净架构方法的示例项目,并且我在将单个转换为另一个时遇到了一些困难。

我有改装服务(单人):

@GET("nearbysearch/json") fun getNearbyPlaces(@Query("type") type: String, @Query("location") location: String, @Query("radius") radius: Int): Single<GooglePlacesNearbySearchResult>

我在我的存储库实现中使用它:

override fun getNearbyPlaces(type: String, location: String, radius: Int): Single<List<Place>> {
    return googlePlacesApi.getNearbyPlaces(type, location, radius)
        .subscribeOn(Schedulers.io())
        .observeOn(Schedulers.computation())
        .doOnSuccess { googlePlacesNearbySearchResult -> nearbyPlaceListResultMapper.transform(googlePlacesNearbySearchResult) }
}

在这个单曲中,我想将我的Single<GooglePlacesNearbyResultSearch>变换为Single<List<Place>>,我想用我的映射器NearbyPlaceListResultMapper做到这一点

问题是我最终没有成功使用Single<List<Place>>。我可以将它转换为Observable或Completable但不是Single。

任何人都可以帮助我以更干净的方式拥有它吗?

谢谢

rx-java retrofit2 reactivex clean-architecture rx-kotlin
1个回答
0
投票

假设nearbyPlaceListResultMapper.transform返回类型List<Place>>,您可以使用map操作。

fun getNearbyPlaces(type: String, location: String, radius: Int): Single<List<Place>> {
    return googlePlacesApi.getNearbyPlaces(type, location, radius)
        .subscribeOn(Schedulers.io())
        .map { nearbyPlaceListResultMapper.transform(it) }
        .observeOn(Schedulers.computation())
}
© www.soinside.com 2019 - 2024. All rights reserved.