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

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

我正在尝试填写一份列表,其中包含有关机场的详细信息。首先,我将获得满足特定条件的机场列表,并获取列表中每个项目的详细信息。最后,我将返回填充列表。

这就是我所拥有的:

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())
            }
    }

上面的代码工作正常,但它在列表中发出了每个项目的可观察值。我想知道如何更改它以便首先填充列表,然后在提取过程完成时返回它。

提前致谢。

android kotlin system.reactive
1个回答
1
投票

是否需要使用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")

“它在列表中发出了一个可观察的每个项目” - flatmap将为每个项目发出。使用toList()意味着它“返回发出单个项目的单个列表,由有限源ObservableSource发出的所有项目组成的列表”。

© www.soinside.com 2019 - 2024. All rights reserved.