isar / isar/hive

Unhandled Exception: HiveError

Open
#611 0 comments 0 reactions 1 assignee Claimed by @simc View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Steps to Reproduce**
It happen on first time on test phone after couple of week using hive. The database size is about 60-100mb. The size is because I store list of objects for offline use. So today when i tried to open my app, it hang where hive is initializing. Open and close multiple time, reinstall the app. It just hang there. I could not check the error because the phone is not run from the laptop. But installed with apk.

So I acted without thinking further, I deleted hive database, and my app able to load normally but without previous saved setting in hive. And then It just came to my mind, I should save those hive files for debugging.. Too late..

I came up with my own way.. On phone, I compress the two files, database.hive and database.lock into database.hive.zip and database.lock.zip. Then I delete the real files, and rename the zip files into real files.

Using flutter hot restart, hive able to detect corrupt file on first refresh. But when i did hot restart on 2nd time, I get below error:
*********************************************************
Performing hot restart...
Restarted application in 2,499ms.
I/flutter ( 5381): Recovering corrupted box.

Performing hot restart...
Restarted application in 1,548ms.
E/flutter ( 5381): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: HiveError: This should not happen. Please
open an issue on GitHub.
*********************************************************
3rd and 4th hard restart seem it be working again.

I tried this couple of time, compress and renaming. It always error on 2nd hot refresh.
It not sure if this similar to the problem I having on the test phone. But I hope this helps to look where the bug is.

********************************* Code **
void init() async {
PermissionStatus status = await Permission.storage.status;
await Permission.storage.request();
status = await Permission.storage.status;

if (status.isGranted) {
if (await dirExist(dbpath) && await dirExist(dbpath + '/images')) {
// There a time database curropted. This is to prevent app from not able to load.
// although hive able to detect corrupt db, this is just another prevention.
try {
Hive..init(dbpath);
_initialized = true;

_welcome = await gGet('welcome') == null ? true : await gGet('welcome');
//... lots more to load
notifyListeners();
await Future.delayed(const Duration(milliseconds: 1000));
_splashed = false;
notifyListeners();
connect();
} catch (e) {
deleteFile(dbpath + '/database.hive');
deleteFile(dbpath + '/database.lock');
await Future.delayed(const Duration(milliseconds: 500));
init();
}
} else {
await dirCreate(dbpath);
await Future.delayed(const Duration(milliseconds: 100));
await dirCreate(dbpath + '/images');
await Future.delayed(const Duration(milliseconds: 100));
init();
}
} else if (status.isDenied || status.isPermanentlyDenied) {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
} else {
await Future.delayed(const Duration(milliseconds: 500));
init();
}
}
********************************* Code **
Note: I just added "try catch", to see how it goes. But since hive does not throw an error, it useless to have these.
final dbpath = '/storage/emulated/0/project';

**Version**
- Platform: Android, Window
- Flutter version: 2.0.3
- Hive version: 2.0.0

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.