平面图操作后获取列表 >

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

Android Studio 3.6.3

我正在使用RxJava将一些记录插入到天气预测表中。

这些表之间存在外键关系。预报表中的天气为description,而weatherId是预报表中的外键。

我写了评论以解释我在做什么

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

只是想知道为什么我在List<Single<WeatherTable>>中得到了onSuccess。我认为应该是List<WeatherTable>

只是一些问题:

  1. 为什么是List<Single<WeatherTable>>而不是List<WeatherTable>
  2. 我可以用List<Single<WeatherTable>>做什么?

非常感谢您的任何建议,

android kotlin rx-java2
2个回答
3
投票

更改

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

to

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

2
投票

[您的问题在flatMap中,您将List用作map(kotlin函数)至List<Single>,这意味着Single.just()将转换为Single<List<Single>>易于修复,可在更改方法getWeatherById中返回weatherDaoWeatherTable而非Single<WeatherTable>

Single.just(forecastList.map {
                    databaseService.weatherDao().getWeatherById(it.weatherId)
                })
© www.soinside.com 2019 - 2024. All rights reserved.