在使用 RX 返回之前填充一些列表

Populate some list before returning it with RX

我正在尝试用有关机场的详细信息填充列表。首先,我获取满足特定条件的机场列表,并获取列表中每个项目的详细信息。最后,我 return 填充了填充列表。

这是我拥有的:

override fun createObservable(params: String): Flowable<List<AirportsEntity>> {
        val destinationAirports = mutableSetOf<AirportsEntity>()
        return this.searchFlightRepository.getDestinationsByCode(params)
            .flatMap {
                Flowable.fromIterable(it)
            }
            .flatMap {
                this.searchFlightRepository.getAirportByCode(it.destination)
            }
            .flatMap {
                destinationAirports.add(it)
                Flowable.just(destinationAirports.toList())
            }
    }

上面的代码工作得很好,但它在列表中为每个项目发出一个可观察的。我想知道如何更改它以便首先填充列表,然后在获取过程完成后 return 它。

提前致谢。

有必要使用Flowable吗?

这样的东西可能更合适:

    private val destinations = listOf("1", "2", "3", "4")

    fun getAirportDestinations(airportCode: String): Single<List<String>> =
            Observable.just(destinations)
                    .flatMapIterable { it }
                    .flatMapSingle { getAirportByCode(it) }
                    .toList()

    private fun getAirportByCode(destinationCode: String): Single<String> =
            Single.just("ABC1")

"it's emitting an observable per item inside the list" - flatmap 将为每个项目发出。 toList() 的使用将意味着它“Returns 发出单个项目的 Single,一个由有限源 ObservableSource 发出的所有项目组成的列表。