【发布时间】:2018-06-12 13:21:25
【问题描述】:
我的存储库中有一个getPlaces 方法:
override fun getPlaces(filter: FilterRequest): Flowable<List<Place>> {
return from(placesApi.filter(filter))
.doOnSuccess {
placesDao.savePlaces(it)
}
.flatMapPublisher { it ->
placesDao.getPlaces(it.map { it.placeId })
}
}
此方法从 api 收集结果,然后将结果保存在数据库中并返回一个 flowable,其中包含通过 id 从数据库中检索到的位置为Flowable:
@Query("select * from Places where placeId in (:placesIds)")
fun getPlaces(placesIds: List<String>) : Flowable<List<Place>>
现在,每次我更改其中一个对象时,我都可以在整个应用程序中看到更改。
现在我想将这些结果与与当前位置的距离相结合,如下所示:
override fun addDistanceToPlaces(req: Flowable<List<Place>>): Flowable<List<Place>> {
return req
.zipWith(getLastLocation().toFlowable(BackpressureStrategy.LATEST),
BiFunction<List<Place>, Location, List<Place>> { places, location ->
places.forEach {
var placeLocation = Location(it.placeName)
placeLocation.latitude = it.latitude
placeLocation.longitude = it.longitude
it.distance = location.distanceTo(placeLocation)
}
places.sortedBy {
it.distance
}
})
.onErrorResumeNext { t: Throwable ->
req
}
}
这可行,但是如果我应用它,我会丢失 Room 的“更新”;更改不会通知观察者,所以我必须手动刷新。
为什么会这样? zip 不应该只是结合两个来源的排放吗?
【问题讨论】:
标签: android rx-java2 android-room