如何在 RxJava 中结合 Completable 和 Single

How to combine Completable with Single in RxJava

我正在构建一个缓存来自 Internet 的数据的应用程序,当 phone 处于离线状态时,它将显示离线项目(此功能按预期工作)。现在我很难包含刷新选项(我基本上会删除缓存中的项目并尝试获取更新的项目)。我有两个问题:

  1. 我不确定如何将 Completable 与 Single 组合,它给我一个错误 none 可以使用提供的参数调用以下函数
  2. 我不确定如何将参数传递给 andThen 运算符中的函数 getWeather。

我的代码:

WeatherRepository

fun deleteWeatherForecast(lat : Double, lng: Double) : Completable
{
    return lWeatherRepo.deleteWeatherForecast(lat,lng)
            .andThen(rWeatherRepo::getWeather(lat,lng))
        .subscribeOn(Schedulers.io())
}

本地天气库

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

我选择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())
}