computeWithDatabase<T> abstract method

Future<T> computeWithDatabase<T>(
  1. Future<T> compute(
    1. Database db
    )
)

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();
      }
    });
  });
}

Implementation

Future<T> computeWithDatabase<T>(
    Future<T> Function(sqlite.Database db) compute);