fix: Resolve SQLITE_BUSY by enabling WAL and fixing isolate init

This commit is contained in:
Max 2026-02-03 14:44:39 +08:00
parent 14161727d5
commit 4cc822c4f6
2 changed files with 29 additions and 10 deletions

View File

@ -256,16 +256,28 @@ Future<void> _storageWorkerEntry(_WorkerInit init) async {
}
final commandPort = ReceivePort();
init.responsePort.send(<String, Object?>{
'type': _msgReady,
'sendPort': commandPort.sendPort,
});
final storage = switch (init.backend) {
IsolateStorageBackend.sqlite => SqfliteEventStorage(),
IsolateStorageBackend.memory => _MemoryEventStorage(),
};
try {
await storage.init();
} on Object catch (e, st) {
Logger.error('Isolate 存储初始化失败', e, st);
// ready ready /
// isolate isolate
// 退
commandPort.close();
return;
}
// ready
init.responsePort.send(<String, Object?>{
'type': _msgReady,
'sendPort': commandPort.sendPort,
});
await for (final message in commandPort) {
if (message is! Map) {

View File

@ -36,8 +36,7 @@ class SqfliteEventStorage implements EventStorage {
return;
}
final directory =
await (_documentsDirectoryProvider?.call() ??
final directory = await (_documentsDirectoryProvider?.call() ??
getApplicationDocumentsDirectory());
final dbPath = p.join(directory.path, DbConstants.dbName);
@ -56,6 +55,15 @@ class SqfliteEventStorage implements EventStorage {
}
},
// coverage:ignore-end
onConfigure: (Database db) async {
// WAL SQLITE_BUSY
// PRAGMA journal_mode 使 execute Android rawQuery
await db.rawQuery('PRAGMA journal_mode = WAL;');
},
onOpen: (Database db) async {
// Store DB
await _createTables(db);
},
),
);
@ -72,8 +80,7 @@ class SqfliteEventStorage implements EventStorage {
);
''');
const createIndexSql =
'CREATE INDEX IF NOT EXISTS idx_events_create_time '
const createIndexSql = 'CREATE INDEX IF NOT EXISTS idx_events_create_time '
'ON $_tableEvents(create_time);';
await db.execute(createIndexSql);
}