平面图操作后获取列表<Single<Weather>>
Getting a List<Single<Weather>> after a flatmap operation
Android Studio 3.6.3
我正在使用 RxJava 将一些记录插入 weather 和 forecast table.
这table之间存在外键关系。 Forecast Table 有天气 description
,而 weatherId
是 Forecast table.
中的外键
我写了评论解释我在做什么
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) }
)
}
这是天气的 daoTable
@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<Single<WeatherTable>>
。我觉得应该是 List<WeatherTable>
.
只是一些问题:
- 为什么是
List<Single<WeatherTable>>
而不是
List<WeatherTable>
?
- 我可以用
List<Single<WeatherTable>>
做什么?
非常感谢您的任何建议,
改变
.flatMap { forecastList ->
Single.just(forecastList.map {
databaseService.weatherDao().getWeatherById(it.weatherId)
})
}
至
.flatMapIterable { forecastList -> forecastList }
.flatMap {
databaseService.weatherDao().getWeatherById(it.weatherId).toObservable()
}
.toList()
你的问题是在 flatMap 中你使用 List
并且你使用 map
(kotlin 函数)到 List<Single>
这意味着 Single.just()
将被转换为 Single<List<Single>>
很容易修复你将 weatherDao
中方法 getWeatherById
的 return 更改为 WeatherTable
而不是 Single<WeatherTable>
Single.just(forecastList.map {
databaseService.weatherDao().getWeatherById(it.weatherId)
})
Android Studio 3.6.3
我正在使用 RxJava 将一些记录插入 weather 和 forecast table.
这table之间存在外键关系。 Forecast Table 有天气 description
,而 weatherId
是 Forecast table.
我写了评论解释我在做什么
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) }
)
}
这是天气的 daoTable
@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<Single<WeatherTable>>
。我觉得应该是 List<WeatherTable>
.
只是一些问题:
- 为什么是
List<Single<WeatherTable>>
而不是List<WeatherTable>
? - 我可以用
List<Single<WeatherTable>>
做什么?
非常感谢您的任何建议,
改变
.flatMap { forecastList ->
Single.just(forecastList.map {
databaseService.weatherDao().getWeatherById(it.weatherId)
})
}
至
.flatMapIterable { forecastList -> forecastList }
.flatMap {
databaseService.weatherDao().getWeatherById(it.weatherId).toObservable()
}
.toList()
你的问题是在 flatMap 中你使用 List
并且你使用 map
(kotlin 函数)到 List<Single>
这意味着 Single.just()
将被转换为 Single<List<Single>>
很容易修复你将 weatherDao
中方法 getWeatherById
的 return 更改为 WeatherTable
而不是 Single<WeatherTable>
Single.just(forecastList.map {
databaseService.weatherDao().getWeatherById(it.weatherId)
})