Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the cache #32

Open
alirezaeiii opened this issue Aug 15, 2024 · 3 comments
Open

Updating the cache #32

alirezaeiii opened this issue Aug 15, 2024 · 3 comments

Comments

@alirezaeiii
Copy link

alirezaeiii commented Aug 15, 2024

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
Copy link
Contributor

saeedishayan76 commented Sep 20, 2024

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
Copy link
Author

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants