Open alirezaeiii opened 3 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 .
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.
I misunderstood your question. What you said is exactly right.
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?