Search code examples
androidkotlinrx-java2

Getting a List<Single<Weather>> after a flatmap operation


Android Studio 3.6.3

I am using RxJava to insert some records to into a weather and forecast table.

There is a foreign key relationship between these tables. Forecast Table has a weather description, And the weatherId is a foreign key in the Forecast table.

I have written comments explaining what I am doing

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

This is the dao for 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>
}

Just wondering why am i getting a List<Single<WeatherTable>> in the onSuccess. I think it should be a List<WeatherTable>.

Just some questions:

  1. Why is it a List<Single<WeatherTable>> and not a List<WeatherTable>?
  2. What can I do with a List<Single<WeatherTable>>?

Many thanks for any suggestions,


Solution

  • Change

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

    to

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