skydoves / pokedex-compose

🗡️ Pokedex Compose demonstrates modern Android development with Jetpack Compose, Hilt, Coroutines, Flow, Jetpack (Room, ViewModel), and Material Design based on MVVM architecture.
Apache License 2.0
749 stars 121 forks source link

Updating the cache #32

Open alirezaeiii opened 3 months ago

alirezaeiii commented 3 months ago

Hi thank you for this helpful sample, I see in your repositories you get data from cache, and if it is not null or empty, you emit that data. Is there somewhere in the code that you update cache?

saeedishayan76 commented 2 months ago

Hi, here is a part of HomeRepoImpl in home feature :

pokemons = pokemonDao.getPokemonList(page).asDomain()
    if (pokemons.isEmpty()) {
      /**
       * fetches a list of [Pokemon] from the network and getting [ApiResponse] asynchronously.
       * @see [suspendOnSuccess](https://github.com/skydoves/sandwich#apiresponse-extensions-for-coroutines)
       */
      val response = pokedexClient.fetchPokemonList(page = page)
      response.suspendOnSuccess {
        pokemons = data.results
        pokemons.forEach { pokemon -> pokemon.page = page }
        pokemonDao.insertPokemonList(pokemons.asEntity())
        emit(pokemonDao.getAllPokemonList(page).asDomain())
      }.onFailure { // handles the all error cases from the API request fails.
        onError(message())
      }
    } else {
      emit(pokemonDao.getAllPokemonList(page).asDomain())
    }

in this part of code val response = pokedexClient.fetchPokemonList(page = page) response.suspendOnSuccess { pokemons = data.results pokemons.forEach { pokemon -> pokemon.page = page } pokemonDao.insertPokemonList(pokemons.asEntity()) emit(pokemonDao.getAllPokemonList(page).asDomain()) }.onFailure { // handles the all error cases from the API request fails. onError(message()) } data fetched from server and store in DB .

alirezaeiii commented 2 months ago

Yes, it stores in DB once since it checks pokemons.isEmpty() and if it is not empty, it always emit data from database and never sync with server again, as I understand.

saeedishayan76 commented 2 months ago

I misunderstood your question. What you said is exactly right.