Replies: 2 comments
-
I'm not aware of any difference between Dart and Flutter that might cause this. I see however that Two possible solutions are:
return DatabaseConnection.delayed(Future(() async {
return NativeDatabase.createBackgroundConnection(
File(await _getDatabasePath()),
logStatements: false,
readPool: 4,
setup: (db) async {
db
..execute('PRAGMA journal_mode = WAL;')
..execute('PRAGMA foreign_keys = ON;')
..execute('PRAGMA encoding = "UTF-8";');
},
);
})); Something worth mentioning is that final class DriftIsolatePool {
final DriftIsolate writer;
final List<DriftIsolate> readers;
DriftIsolatePool({required this.writer, required this.readers});
Future<DatabaseConnection> connect() async {
final writingConnection = await writer.connect();
final readConnections = await Future.wait([
for (final readIsolate in readers) readIsolate.connect(),
]);
return DatabaseConnection(
MultiExecutor.withReadPool(
reads: readConnections,
write: writingConnection,
),
streamQueries: writingConnection.streamQueries,
connectionData: writingConnection.connectionData,
);
}
Future<void> close() async {
await writer.shutdownAll();
await Future.wait([for (final reader in readers) reader.shutdownAll()]);
}
static Future<DriftIsolatePool> spawn(File file, {int readSize = 4}) async {
final writer = await _spawnForPool(file.path, false);
final readers = await Future.wait(
[for (var i = 0; i < readSize; i++) _spawnForPool(file.path, true)],
);
return DriftIsolatePool(writer: writer, readers: readers);
}
static Future<DriftIsolate> _spawnForPool(String path, bool reader) {
return DriftIsolate.spawn(() {
return NativeDatabase(
File(path),
enableMigrations: !reader,
setup: (db) {
db
..execute('PRAGMA journal_mode = WAL;')
..execute('PRAGMA foreign_keys = ON;')
..execute('PRAGMA encoding = "UTF-8";');
},
);
});
}
}
final _pool = Future(() async {
final path = 'test.db'; //_getDatabasePath();
return DriftIsolatePool.spawn(File(path));
}); To connect the main isolate, use static QueryExecutor _openConnection() {
return DatabaseConnection.delayed(Future(() async {
final pool = await _pool;
return await pool.connect();
}));
} And to connect other isolates: Future<void> processData(IsolatePool<int> pool) async {
final driftPool = await _pool;
final workers = <Future<int>>[];
final reports = await db.getReportsToProcess();
for (final report in reports) {
workers.add(pool.scheduleJob(ProcessReport<int>(report.id, driftPool)));
}
await workers.wait;
}
class ProcessReport<T> extends PooledJob<T> {
final String reportId;
final DriftIsolatePool pool;
ProcessReport(this.reportId, this.pool);
@override
Future<T> job() async {
print('Processing report: $reportId');
final db = AppDatabase(await pool.connect());
print('db connected');
try {
final report = await db.getReportById(reportId);
if (report == null) throw Exception('report not found: $feedId');
// Process Report
} finally {
await db.close();
}
return 1;
}
} |
Beta Was this translation helpful? Give feedback.
-
WOW, thanks for take the time for the explanation and the code sample!!... I tried your Thanks!! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm working with isolatePool2 to have some workers and each job receive a db connection.
This code is working just great in plain Dart. Now I moved it to Flutter and I see the that db connection never happens, the print line 'db connected' is never show.
What I'm doing wrong? is there some differences between Dart and Flutter that need to take into account?
TIA
:: nelson
Beta Was this translation helpful? Give feedback.
All reactions