computeWithDatabase<T> abstract method
Run a function within a database isolate, with direct synchronous access to the underlying database.
Using closures must be done with care, since values are sent over to the database isolate. To be safe, use this from a top-level function, taking only required arguments.
The database may only be used within the callback, and only until the returned future returns. If it is used outside of that, it could cause unpredictable issues in other transactions.
Example:
Future<void> largeBatchInsert(SqliteConnection connection, List<List<Object>> rows) {
await connection.writeTransaction((tx) async {
await tx.computeWithDatabase((db) async {
final statement = db.prepare('INSERT INTO data(id, value) VALUES (?, ?)');
try {
for (var row in rows) {
statement.execute(row);
}
} finally {
statement.dispose();
}
});
});
}
This is not supported on the web, where synchronous access to the database is only available in workers.
On native platforms, compute runs in a background isolate if
SqliteOptions.useBackgroundIsolates is enabled (the default). Uncaught
errors from background isolates are reported if they occur before the
future completes.
If SqliteOptions.useBackgroundIsolates is disabled, the function runs on
the calling isolate instead. No error zone is set up in that case.
Implementation
Future<T> computeWithDatabase<T>(
Future<T> Function(sqlite.CommonDatabase db) compute);