Skip to content
Permalink
Browse files

Use a single database write for inserting all Pokemon instances

  • Loading branch information
zsmb13 committed Jan 7, 2020
1 parent 3ebd4a2 commit 07afada2b3d7b0cca36057706f0750434691475e
@@ -14,7 +14,7 @@ interface PokemonDAO {
fun all(): LiveData<List<Pokemon>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun add(vararg pokemon: Pokemon)
fun add(pokemon: List<Pokemon>)

@Query("DELETE FROM pokemon")
fun deleteAll()
@@ -9,6 +9,7 @@ import dev.marcosfarias.pokedex.repository.APIService
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.concurrent.thread

class PokedexViewModel : ViewModel() {

@@ -25,13 +26,10 @@ class PokedexViewModel : ViewModel() {
call: Call<List<Pokemon>?>?,
response: Response<List<Pokemon>?>?
) {
response?.body()?.let {
val pokemons: List<Pokemon> = it
Thread(Runnable {
for (pokemon in pokemons) {
pokemonDAO.add(pokemon)
}
}).start()
response?.body()?.let { pokemons: List<Pokemon> ->
thread {
pokemonDAO.add(pokemons)
}
}
}

0 comments on commit 07afada

Please sign in to comment.
You can’t perform that action at this time.