feat: Implement batch insertion for Pokémon and utilize it for more efficient initial data synchronization.
This commit is contained in:
@@ -38,18 +38,16 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
||||
|
||||
final count = await PokedexDatabase.getCaughtCount();
|
||||
|
||||
// Check if database is empty for initial sync
|
||||
// Check if database needs sync (less than 1025 pokemon)
|
||||
List<Pokemon> localData = await PokedexDatabase.getPokemonList();
|
||||
if(localData.isEmpty) {
|
||||
if (localData.length < 1025) {
|
||||
try {
|
||||
final List<Pokemon> remoteData = await PokemonApi.getAllPokemon();
|
||||
// Insert all
|
||||
for (var p in remoteData) {
|
||||
await PokedexDatabase.insertPokemon(p);
|
||||
}
|
||||
// Insert all missing pokemon using batch for performance
|
||||
await PokedexDatabase.batchInsertPokemon(remoteData);
|
||||
localData = await PokedexDatabase.getPokemonList();
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
debugPrint('Sync Error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user