0
votes

I'm building an application that is caching data from the internet, when the phone is offline it will show the offline items(this functionality works as intended). Now I have a difficulty to include a refresh option(I will basically will delete the items that I have in the cache and try to fetch more recent items). I have two problems:

  1. I'm not sure how to combine Completable with Single it gives me an error none of the following functions can be called with the arguments supplied
  2. I'm not sure how to pass arguments to a function getWeather in andThen operator.

My Code:

WeatherRepository

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

LocalWeatherRepository

fun deleteWeatherForecast(lat: Double, lng: Double) : Completable
{
    return weatherDao.deleteForecastByLocation(lat,lng)
}

RemoteWeatherRepository

fun getWeather(lat: Double, lng: Double): Single<Weather> {
    val locationStr = String.format("%f,%f",lat,lng)
    return weatherService.getWeatherForecastResponse(API_KEY,locationStr)
}

I chose Completable because I want to wait till the deletion will be completed and fetch the next it

1

1 Answers

0
votes

You could rewrite your WeatherRepository like this

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