Skip to content

Commit 8614740

Browse files
committed
Flutter red b update
1 parent 7c533cb commit 8614740

File tree

3 files changed

+19
-78
lines changed

3 files changed

+19
-78
lines changed

lib/src/bridge/local_db_bridge.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LocalDbBridge extends LocalSbRequestImpl {
3838
Pointer<AppDbState>? _dbInstance; // Cambiado de late a nullable
3939
String? _lastDatabaseName; // Almacena el último nombre de base de datos utilizado
4040
bool _hotRestartDetected = false; // Flag para detectar hot restart
41+
bool _isInitialized = false; // Flag para saber si ya se inicializó completamente
4142

4243
/// Public getter for hot restart detected flag (for debugging/recovery purposes)
4344
bool get hotRestartDetected => _hotRestartDetected;
@@ -69,9 +70,22 @@ class LocalDbBridge extends LocalSbRequestImpl {
6970
Future<void> initialize(String databaseName) async {
7071
try {
7172
log('Initializing DB on platform: ${Platform.operatingSystem}');
73+
log('Is already initialized: $_isInitialized');
7274

7375
_lastDatabaseName = databaseName;
7476

77+
// Si ya está inicializado (hot restart), solo reinicializar la instancia de DB
78+
if (_isInitialized) {
79+
log('Hot restart detected - skipping library and function binding');
80+
_hotRestartDetected = false; // Reset flag
81+
82+
final appDir = await getApplicationDocumentsDirectory();
83+
await _init('${appDir.path}/$databaseName');
84+
log('Database reinitialized successfully after hot restart');
85+
return;
86+
}
87+
88+
// Primera inicialización completa
7589
if(_lib == null) {
7690
/// Initialize native library.
7791
_lib = await CurrentPlatform.loadRustNativeLib();
@@ -101,7 +115,10 @@ class LocalDbBridge extends LocalSbRequestImpl {
101115

102116
/// Initialize database with default route and database name.
103117
await _init('${appDir.path}/$databaseName');
104-
log('Database initialized successfully');
118+
119+
// Marcar como inicializado completamente
120+
_isInitialized = true;
121+
log('Database initialized successfully (first time)');
105122

106123
} catch (e, stack) {
107124
log('Error initializing database: $e');

lib/src/local_db.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ class LocalDB {
2424
///
2525
/// Throws an exception if initialization fails
2626
static Future<void> init({required String localDbName}) async {
27-
try {
28-
await LocalDbBridge.instance.initialize(localDbName);
29-
} catch (e) {
30-
log('Initial database initialization failed: $e');
31-
log('This may be due to hot restart, attempting recovery...');
32-
33-
// Mark hot restart detection and try again
34-
LocalDbBridge.instance.hotRestartDetected = true;
35-
await LocalDbBridge.instance.initialize(localDbName);
36-
}
27+
await LocalDbBridge.instance.initialize(localDbName);
3728
}
3829

3930
/// Avoid to use on production.

test/flutter_local_db_test.dart

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter_local_db/src/service/local_db_result.dart';
22
import 'package:flutter_test/flutter_test.dart';
33
import 'package:flutter_local_db/flutter_local_db.dart';
4-
import 'package:flutter_local_db/src/bridge/local_db_bridge.dart';
54

65
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
76

@@ -684,70 +683,4 @@ void main() {
684683
});
685684
});
686685

687-
group('Hot Restart Tests', () {
688-
test('Should persist data after simulated hot restart', () async {
689-
// First, create some test data
690-
final testData1 = {'name': 'John', 'age': 30};
691-
final testData2 = {'name': 'Jane', 'age': 25};
692-
693-
await LocalDB.Post('user-1', testData1);
694-
await LocalDB.Post('user-2', testData2);
695-
696-
// Verify data exists before "hot restart"
697-
final beforeRestart = await LocalDB.GetAll();
698-
expect(beforeRestart.isOk, true);
699-
expect(beforeRestart.data!.length, 2);
700-
701-
// Simulate hot restart by forcing reinitialization
702-
// This mimics what happens during actual hot restart
703-
LocalDbBridge.instance.hotRestartDetected = true;
704-
705-
// Try to access data after marking hot restart
706-
final afterRestart = await LocalDB.GetAll();
707-
708-
// Debug information
709-
print('After restart result: ${afterRestart.isOk}');
710-
if (afterRestart.isErr) {
711-
print('Error after restart: ${afterRestart.errorOrNull}');
712-
}
713-
714-
expect(afterRestart.isOk, true);
715-
expect(afterRestart.data!.length, 2);
716-
717-
// Verify specific data is still there
718-
final user1 = await LocalDB.GetById('user-1');
719-
expect(user1.isOk, true);
720-
expect(user1.data?.data['name'], 'John');
721-
expect(user1.data?.data['age'], 30);
722-
723-
final user2 = await LocalDB.GetById('user-2');
724-
expect(user2.isOk, true);
725-
expect(user2.data?.data['name'], 'Jane');
726-
expect(user2.data?.data['age'], 25);
727-
});
728-
729-
test('Should handle multiple hot restarts gracefully', () async {
730-
// Create initial data
731-
await LocalDB.Post('persistent-key', {'counter': 1});
732-
733-
// Simulate multiple hot restarts
734-
for (int i = 2; i <= 5; i++) {
735-
LocalDbBridge.instance.hotRestartDetected = true;
736-
737-
// Update the counter
738-
await LocalDB.Put('persistent-key', {'counter': i});
739-
740-
// Verify data persists
741-
final result = await LocalDB.GetById('persistent-key');
742-
expect(result.isOk, true);
743-
expect(result.data?.data['counter'], i);
744-
}
745-
746-
// Final verification
747-
final finalResult = await LocalDB.GetAll();
748-
expect(finalResult.isOk, true);
749-
expect(finalResult.data!.length, 1);
750-
expect(finalResult.data!.first.data['counter'], 5);
751-
});
752-
});
753686
}

0 commit comments

Comments
 (0)