-
Hello! My app has a feature which sends queued data from database table in foreground service in separate isolate via I'm using drift with
The foreground service isolate is started when some data was queued to send on ui isolate. I'm using ports to establish connection to Drift isolate in foreground service isolate like this: UI isolate registers port: static const driftIsolatePortName = 'drift-isolate-port';
static Future<void> startService(AppDBService database) async {
final connection = await database.serializableConnection();
IsolateNameServer.removePortNameMapping(driftIsolatePortName);
IsolateNameServer.registerPortWithName(
connection.connectPort,
driftIsolatePortName,
);
} Then I'm starting foreground service isolate and then I'm creating database attached to Drift isolate like this: Future<ScoutDBService> _initDatabase() async {
final sendPort = IsolateNameServer.lookupPortByName(
BackgroundQueueServiceManager.driftIsolatePortName,
)!;
final driftIsolate = DriftIsolate.fromConnectPort(sendPort);
final dbConnection = await driftIsolate.connect();
final database = LazyDatabase(() => dbConnection);
return AppDBService(database: database);
} This isolate send data from database and deletes sent entries. It's all fine, but I want to update data in UI from database. I've subscribed to changes on UI thread, but the stream sends only one event when I subscribe and then nothing: void init() {
_requestsSubscription =
_requestDao.getAllRequestsWithTasksStream().listen(_updateRequestsList);
} It works fine if send data on UI isolate, but with current implementation it doesn't update the stream. As I see in documentation, when I use same drift isolate on both my isolates, query streams should work correctly when data is updated from either isolate, but that's not the case here. Have I done something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The
If you need ScoutDBService _initDatabase() {
final sendPort = IsolateNameServer.lookupPortByName(
BackgroundQueueServiceManager.driftIsolatePortName,
)!;
return AppDBService(database: DatabaseConnection.delayed(Future(() async {
final driftIsolate = DriftIsolate.fromConnectPort(sendPort);
return await driftIsolate.connect();
})));
} |
Beta Was this translation helpful? Give feedback.
The
LazyDatabase
wrapper on the foreground service isolate stands out. In drift, the stream query store responsible for distributing table updates is part of theDatabaseConnection
class (which essentially wraps theQueryExecutor
for running statements and theStreamQueryStore
).DriftIsolate.connect()
gives you aDatabaseConnection
, but using aLazyDatabase
(which is only a standaloneQueryExecutor
) will essentially set up a local stream query store instead of using the one from the drift isolate.In your case, the
LazyDatabase
wrapper isn't necessary at all since_initDatabase()
is already async, just pass thedbConnection
to theAppDBService
constructor directly.If you need
_initDatabase
…