DriftIsolate class
Defines utilities to run drift in a background isolate. In the operation mode created by these utilities, there's a single background isolate doing all the work. Any other isolate can use the connect method to obtain an instance of a GeneratedDatabase class that will delegate its work onto a background isolate. Auto-updating queries, and transactions work across isolates, and the user facing api is exactly the same.
Please note that, while running drift in a background isolate can reduce lags in foreground isolates (thus removing UI jank), the overall database performance will be worse. This is because result data is not available directly and instead needs to be copied from the database isolate. Thanks to recent improvements like isolate groups in the Dart VM, this overhead is fairly small and using isolates to run drift queries is recommended where possible.
The easiest way to use drift isolates is to use
NativeDatabase.createInBackground
, which is a drop-in replacement for
NativeDatabase
that uses a DriftIsolate under the hood.
Also, be aware that this api is not available on the web.
See also:
- Isolate, for general information on multi threading in Dart.
- The detailed documentation, which provides example codes on how to use this api.
Constructors
- DriftIsolate.fromConnectPort(SendPort connectPort, {bool? serialize})
-
Creates a DriftIsolate talking to another isolate by using the
connectPort
. - DriftIsolate.inCurrent(DatabaseOpener opener, {bool killIsolateWhenDone = false, bool serialize = false, bool shutdownAfterLastDisconnect = false, ReceivePort? port, void beforeShutdown()?})
-
Creates a DriftIsolate in the Isolate.current isolate. The returned
DriftIsolate is an object than can be sent across isolates - any other
isolate can then use DriftIsolate.connect to obtain a special database
connection which operations are all executed on this isolate.
factory
Properties
- connectPort → SendPort
-
The underlying port used to establish a connection with this
DriftIsolate.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- serialize → bool?
-
The flag indicating whether messages between this DriftIsolate
and the DriftServer should be serialized.
final
Methods
-
connect(
{bool isolateDebugLog = false, bool singleClientMode = false, Duration? connectTimeout}) → Future< DatabaseConnection> - Connects to this DriftIsolate from another isolate.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
shutdownAll(
) → Future< void> - Stops the background isolate and disconnects all DatabaseConnections created. If you only want to disconnect a database connection created via connect, use GeneratedDatabase.close instead.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
spawn(
DatabaseOpener opener, {bool serialize = false, Future< Isolate> isolateSpawn<T>(void (T), T) = Isolate.spawn }) → Future<DriftIsolate> - Creates a new DriftIsolate on a background thread.