diff --git a/moor_ffi/README.md b/moor_ffi/README.md index c65f901d..dd223710 100644 --- a/moor_ffi/README.md +++ b/moor_ffi/README.md @@ -96,5 +96,4 @@ In all other project files that use moor apis (e.g. a `Value` class for companio Finally, replace usages of `FlutterQueryExecutor` with `VmDatabase`. -Note that, at the moment, there is no direct counterpart for `FlutterQueryExecutor.inDatabasePath` -and that the async API using a background isolate is not available for moor yet. \ No newline at end of file +Note that, at the moment, there is no direct counterpart for `FlutterQueryExecutor.inDatabasePath`. \ No newline at end of file diff --git a/moor_ffi/lib/src/vm_database.dart b/moor_ffi/lib/src/vm_database.dart index 4699c898..5d143e1c 100644 --- a/moor_ffi/lib/src/vm_database.dart +++ b/moor_ffi/lib/src/vm_database.dart @@ -7,13 +7,18 @@ class VmDatabase extends DelegatedDatabase { /// Creates a database that will store its result in the [file], creating it /// if it doesn't exist. - factory VmDatabase(File file, {bool logStatements = false}) { - return VmDatabase._(_VmDelegate(file), logStatements); + /// + /// If [background] is enabled (defaults to false), the database will be + /// opened on a background isolate. This is much slower, but reduces work on + /// the UI thread. + factory VmDatabase(File file, + {bool logStatements = false, bool background = false}) { + return VmDatabase._(_VmDelegate(file, background), logStatements); } - /// Creates a database won't persist its changes on disk. + /// Creates an in-memory database won't persist its changes on disk. factory VmDatabase.memory({bool logStatements = false}) { - return VmDatabase._(_VmDelegate(null), logStatements); + return VmDatabase._(_VmDelegate(null, false), logStatements); } } @@ -21,8 +26,9 @@ class _VmDelegate extends DatabaseDelegate { BaseDatabase _db; final File file; + final bool background; - _VmDelegate(this.file); + _VmDelegate(this.file, this.background); @override final TransactionDelegate transactionDelegate = const NoTransactionDelegate(); @@ -34,10 +40,18 @@ class _VmDelegate extends DatabaseDelegate { Future get isOpen => Future.value(_db != null); @override - Future open([GeneratedDatabase db]) { + Future open([GeneratedDatabase db]) async { if (file != null) { - _db = Database.openFile(file); + if (background) { + _db = await IsolateDb.openFile(file); + } else { + _db = Database.openFile(file); + } } else { + assert( + !background, + "moor_ffi doesn't support in-memory databases on a background " + 'isolate'); _db = Database.memory(); } versionDelegate = _VmVersionDelegate(_db); diff --git a/moor_ffi/pubspec.yaml b/moor_ffi/pubspec.yaml index 8f290249..3d6be9d4 100644 --- a/moor_ffi/pubspec.yaml +++ b/moor_ffi/pubspec.yaml @@ -1,5 +1,5 @@ name: moor_ffi -description: "Experimental sqlite bindings using dart:ffi" +description: "Provides experimental sqlite bindings using dart:ffi, including a moor executor" version: 0.0.1 author: Simon Binder homepage: https://github.com/simolus3/moor/tree/develop/moor_ffi @@ -10,6 +10,7 @@ environment: dependencies: moor: ">=1.7.0 <2.1.0" + collection: ^1.0.0 dev_dependencies: test: ^1.6.0