I have been searching why inserting alot of data using batch freezes the UI for some time. I have tried Isolate/ Compute but according to this Insert sqlite flutter without freezing the interface it is no longer possible.
I am trying to insert 10k+ rows of data as a whole. It works very well
insertOrUpdate(String tableName, List<dynamic> data) async {
final Database db = (await database)!;
Batch batch = db.batch();
var time = DateTime.now();
for (final element in data) {
batch.insert(
'$tableName',
element.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
var time2 = DateTime.now();
var d = time2.difference(time);
print("Finished ${data.length} in $d");
await batch.commit(noResult: true);
}
"Finished 10228 in 0:00:00.208229"
but the UI freezes for about ~1 second(enought to feel it lag).
As you will see in the code below i am trying to separate batches to 1000 at a time and still it does lag but now in intervals.. and ofcourse it takes much longer to do the process.
insertOrUpdate(String tableName, List<dynamic> data) async {
final Database db = (await database)!;
Batch batch = db.batch();
var time = DateTime.now();
int index = 0;
for (final element in data) {
batch.insert(
'$tableName',
element.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
if (index % 1000 == 0) {
print(index);
await batch.commit(noResult: true);
batch = db.batch();
}
index++;
}
var time2 = DateTime.now();
var d = time2.difference(time);
print("Finished ${data.length} in $d");
await batch.commit(noResult: true);
}
"Finished 10228 in 0:00:04.775841"
Any ideas on how to avoid locking up the UI when the BATCH is commited?