我正在寻找一个采用干净架构方法的示例项目,并且我在将单个转换为另一个时遇到了一些困难。
我有改装服务(单人):
@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。
任何人都可以帮助我以更干净的方式拥有它吗?
谢谢
假设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())
}