首页 > 解决方案 > 当 flowable 发射时从单个发射

问题描述

我有一个活动,您可以根据过滤器和地图的当前视口看到一堆地方。

所以我将这两个来源建模为Flowables

fun boundariesFlowable(): Flowable<Pair<LatLon, LatLon>>
fun filtersFlowable(): Flowable<FilterRequest>

边界发出地图视口的左上角和右下角,过滤器只发出用户应用的过滤器的任何变化。

现在我想观察这两个来源,以便每次它们中的任何一个发生变化时都会执行一次调用。这是我的请求电话:

 fun refreshApplyFilter(id: Int, filter: FilterRequest): Single<FilterResponse>

问题是,我无法弄清楚如何组合这个触发源,或者如何对它们的发射执行调用。

我已尝试仅对其中一个源进行平面映射,但它需要 a Publisher,而我的请求是Single.

标签: androidrx-javarx-java2

解决方案


您可以使用Flowable.combineLatest()运算符来观察多个来源。如果其中任何一个发布新值,将触发函数。

val res: Flowable<FilterResponse> = Flowable.combineLatest(boundariesFlowable(), filtersFlowable(), BiFunction<Pair<LatLon, LatLon>, FilterRequest, FilterRequest> { pair, filter ->
    filter
}).flatMapSingle { filter -> refreshApplyFilter(0, filter) }

推荐阅读