Android Studio 3.6.3
我正在使用RxJava将一些记录插入到天气和天气预报表中。这些表之间存在外键关系。预报表有天气的描述。天气ID是“预测”表中的外键。
我已经写了评论来解释我在做什么
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) }
)
}
只是想知道为什么我在onSuccess中获得了List>。我认为应该是清单
只是一些问题: 1)为什么它是列表>而不是列表? 2)我可以使用List>做什么?
非常感谢您的任何建议,
更改
至