【问题标题】:Getting a List<Single<Weather>> after a flatmap operation平面图操作后获取 List<Single<Weather>>
【发布时间】:2020-05-18 02:03:50
【问题描述】:

Android Studio 3.6.3

我正在使用 RxJava 将一些记录插入到 weatherforecast 表中。

这些表之间存在外键关系。预测表有天气descriptionweatherId 是预测表中的外键。

我已经写了 cmets 来解释我在做什么

private fun createWeatherForecast() {
        val disposable = databaseService
            .weatherDao()
            // Insert the weather description 
            .insert(WeatherTable(icon= "icon", code = (10..1000).shuffled().first(), description = "cloudy with a chance of meatballs"))
            // Insert the Forecast. A weather ID is returned and used as a foregin key in the forecast table.
            .flatMap { weatherId ->
                databaseService.forecastDao().insert(
                    ForecastTable(
                        temp = 45.2F,
                        highTemp = 50.5F,
                        lowTemp = 34.8F,
                        feelLikeMinTemp = 30.5F ,
                        feelLikeMaxTemp = 49.9F,
                        validDate = "today",
                        weatherId = weatherId)
                )
            }
            // Get the forecast weather from the Forecast Table
            .flatMap {
                databaseService.forecastDao().getAllForecast()
            }
            // Get the weather by WeatherID that was inserted in the Forecast table and loop
            .flatMap { forecastList ->
                Single.just(forecastList.map {
                    databaseService.weatherDao().getWeatherById(it.weatherId)
                })
            }
            .subscribeOn(schedulerProvider.backgroundScheduler())
            .observeOn(schedulerProvider.uiScheduler())
            .subscribeBy(
            // This is the confusion as I expect a List<WeatherTable> but instead its List<Single<WeatherTable>>
                onSuccess = {
                    println("Weather forecast $it")
                },
                onError = { println(it.message) }
            )
    }

这是 WeatherTable 的 dao

@Dao
interface WeatherDao : BaseDao<WeatherTable> {
    @Query("SELECT * FROM weatherTable")
    fun getAllWeather(): Single<List<WeatherTable>>

    @Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1")
    fun getWeatherById(id: Long): Single<WeatherTable>

    @Query("SELECT count(*) FROM weatherTable")
    fun count(): Single<Int>
}

只是想知道为什么我在onSuccess 中得到一个List&lt;Single&lt;WeatherTable&gt;&gt;。我觉得应该是List&lt;WeatherTable&gt;

只是一些问题:

  1. 为什么是List&lt;Single&lt;WeatherTable&gt;&gt; 而不是 List&lt;WeatherTable&gt;?
  2. List&lt;Single&lt;WeatherTable&gt;&gt; 可以做什么?

非常感谢您的任何建议,

【问题讨论】:

  • getWeatherById 是 Single 对吗?
  • 是的,看起来是这样的:@Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1") fun getWeatherById(id: Long): Single&lt;WeatherTable&gt;

标签: android kotlin rx-java2


【解决方案1】:

改变

        .flatMap { forecastList ->
            Single.just(forecastList.map {
                databaseService.weatherDao().getWeatherById(it.weatherId)
            })
        }

        .flatMapIterable { forecastList -> forecastList }
        .flatMap {
            databaseService.weatherDao().getWeatherById(it.weatherId).toObservable()
        }
        .toList()

【讨论】:

  • 感谢您解决了我的问题。因此flatMapIterable 将采用从getAllForecast() 返回的 List 列表,并将列表中的每个项目作为ForecastTable?
  • 没错,就是将 Iterable 的 observable 转换为 observable 并发出列表中的所有项。
【解决方案2】:

您的问题在 flatMap 内部,您将 List 使用 map(kotlin 函数)到 List&lt;Single&gt; 这意味着 Single.just() 将转换为 Single&lt;List&lt;Single&gt;&gt; 易于修复您更改方法 getWeatherById 的返回在weatherDaoWeatherTable 而不是Single&lt;WeatherTable&gt;

Single.just(forecastList.map {
                    databaseService.weatherDao().getWeatherById(it.weatherId)
                })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多