Android 如何在RxJava中结合Completable和Single
我正在构建一个从互联网缓存数据的应用程序,当手机处于脱机状态时,它将显示脱机项目(此功能按预期工作)。现在我很难包含刷新选项(我基本上会删除缓存中的项目,并尝试获取最近的项目)。我有两个问题:Android 如何在RxJava中结合Completable和Single,android,kotlin,rx-java2,Android,Kotlin,Rx Java2,我正在构建一个从互联网缓存数据的应用程序,当手机处于脱机状态时,它将显示脱机项目(此功能按预期工作)。现在我很难包含刷新选项(我基本上会删除缓存中的项目,并尝试获取最近的项目)。我有两个问题: 我不知道如何将Completable和Single组合起来,这给了我一个错误 使用提供的参数无法调用以下任何函数 我不知道如何将参数传递给函数getWeather和第二个运算符 我的代码: 天气预报库 fun deleteWeatherForecast(lat: Double, lng: Double)
fun deleteWeatherForecast(lat: Double, lng: Double) : Completable
{
return weatherDao.deleteForecastByLocation(lat,lng)
}
fun getWeather(lat: Double, lng: Double): Single<Weather> {
val locationStr = String.format("%f,%f",lat,lng)
return weatherService.getWeatherForecastResponse(API_KEY,locationStr)
}
LocalWeatherRepository
fun deleteWeatherForecast(lat: Double, lng: Double) : Completable
{
return weatherDao.deleteForecastByLocation(lat,lng)
}
fun getWeather(lat: Double, lng: Double): Single<Weather> {
val locationStr = String.format("%f,%f",lat,lng)
return weatherService.getWeatherForecastResponse(API_KEY,locationStr)
}
RemoteWeatherRepository
fun deleteWeatherForecast(lat: Double, lng: Double) : Completable
{
return weatherDao.deleteForecastByLocation(lat,lng)
}
fun getWeather(lat: Double, lng: Double): Single<Weather> {
val locationStr = String.format("%f,%f",lat,lng)
return weatherService.getWeatherForecastResponse(API_KEY,locationStr)
}
fun getWeather(纬度:双人,液化天然气:双人):单人{
val locationStr=String.format(“%f,%f”,lat,lng)
返回weatherService.getWeatherForecastResponse(API_键,位置str)
}
我之所以选择Completable,是因为我想等到删除完成后再获取下一个,你可以像这样重写你的WeatherRepository
fun deleteWeatherForecast(lat : Double, lng: Double) : Completable
{
return lWeatherRepo.deleteWeatherForecast(lat,lng)
.andThen(rWeatherRepo.getWeather(lat,lng)) // answer question 2
.ignoreElement() // answer question 1, convert single to completable
.subscribeOn(Schedulers.io())
}