0

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?

Paul Grei
  • 369
  • 2
  • 12
  • Hello ! I'm not posting this as an answer since I never worked with it, but you should check the Isolate class. From what I remember, it may help you with this problem. – FDuhen Mar 31 '21 at 18:29
  • problem is that i cant access sqflite from Isolate. This is know problem and is described here https://github.com/flutter/flutter/issues/13937 :/ – Paul Grei Mar 31 '21 at 19:21
  • And did you check the https://pub.dev/packages/moor package ? – FDuhen Mar 31 '21 at 20:25
  • I don't want to rewrite a big chunk of my code just to test if the package doesn't freeze the UI. I am asking for any ideas on why sqflite batch process freezes up the UI and how to try and unfreeze it. Not looking for alternative db manager package. Thanks for suggestions though :) – Paul Grei Apr 01 '21 at 07:09
  • Dart is running on a single Thread, which explains why your huge commit is impacting the rendering of the app. I hope someone will have a solution for you : it triggered my curiosity :) – FDuhen Apr 01 '21 at 07:25
  • @PauliusGreičiūnas did you find any solution to your problem? I have to design a backup/restore module of an app which uses sqflite so i will hit the same hurdle as yours. I was curious if you found any solution to your problem. – irshukhan Jan 06 '22 at 08:16
  • @irshukhan sadly no. I would try to ditch Sqflite in favor of https://pub.dev/packages/hive which looks like has WAY better performance in reading/writing/deleting data than sqflite. – Paul Grei Jan 08 '22 at 11:37

0 Answers0