Program freezes on 31k row inserts #2836
-
I am trying to insert a Bible per verse into a database. There are 31,102 rows and the program will not update. I use batch to build the table of contents and other areas in the following manner, and it works fine. It's the verses that the program freezes on. Any help is greatly appreciated: [Entries are going into the database, just one at a time and the program fully freezes during the inserts]
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Both here and in your SO question, you're not actually using a batch. Here, there aren't any batches at all. And in the snippet you've posted on SO, there's a batch for each element in the list, so there will be 31k batches instead of the single one you probably intended. To use a batch here, start it in the try block and iterate over the list inside of the batch, like this: //receives verses in json format and inserts it into db
Future<Either<Failure, Success>> insertVersesFromJson(
String verseTable, List<Map<String, dynamic>> list) async {
try {
await batch((b) {
for (Map<String, dynamic> json in list) {
final String verseId = json['verse_id'].toString();
// ...
b.insert(verses, VerseCompanion.insert(verseId: verseId, ...));
}
});
return Right<Failure, Success>(Success());
} catch (error) {
errorLog(error.toString(), StackTrace.current);
return Left<Failure, Success>(DatabaseFailure());
}
} If you need to use a custom statement, use To address the freeze: Are you using |
Beta Was this translation helpful? Give feedback.
Both here and in your SO question, you're not actually using a batch. Here, there aren't any batches at all. And in the snippet you've posted on SO, there's a batch for each element in the list, so there will be 31k batches instead of the single one you probably intended.
Also, note that
list.map((...) async {})
will not await the asynchronous mapping at all, so the try/catch block will also not catch errors made by any query.Finally, I recommend not using custom statements when the query can be written with type-safe drift APIs instead. If you want use custom statements, you should almost never use string interpolation in them because they make you vulnerable to something called SQL inj…