hive icon indicating copy to clipboard operation
hive copied to clipboard

I've been working with the Hive Database in Isolate functions and it's hard to manage the database. Because Hive database throws error if you try to use it in Isolates. Do you have any plans to implement multi threading in the Hive flutter.

Open Bahrom2101 opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Version

  • Platform: iOS, Android, Mac, Windows, Linux, Web
  • Flutter version: [e.g. 1.5.4]
  • Hive version: [e.g. 0.5.0]

Bahrom2101 avatar Jun 22 '22 06:06 Bahrom2101

I can't figured out how to open a box with a path but it will work if you open it with bytes.

For example :

final receiver = ReceivePort('myPort');
IsolateNameServer.removePortNameMapping('myPort');
IsolateNameServer.registerPortWithName(receiver.sendPort, 'myPort');

late Box<MyModel> box;
receiver.listen((dynamic data) {
  box = data as Box<MyModel>;
});

Isolate.spawn<Map<String, dynamic>>(
  (data) async {
    final boxName = data['boxName'];
    final portName = data['portName'];
    final port = IsolateNameServer.lookupPortByName(portName);

    final input = InputStream(compressed);
    final decompressed = ZipDecoder().decodeBuffer(input);
    final decompressedBytes = decompressed.fileData(0) as Uint8List;

    final box = await Hive.openBox(
      boxName,
      bytes: decompressedBytes,
    );

    Isolate.exit(port, box);
  },
  {
    'boxName': 'myBox',
    'portName': 'myPort',
    'compressed': compressed, // this is my compressed box that I've read with rootBundle.load()
  },
);

In my case, there are ~40k entries and the box size is ~200mb. The UI blocks during 1-2 seconds during the box content transfert through the port. I've found here https://github.com/dart-lang/language/issues/124#issuecomment-957556498 that Isolate.exit() is mort efficient than doing port.send() (and that's true in my case).

Unfortunately, I still have my UI blocked during some time and lazy box does not fit with my needs

ziadsarour avatar Jun 26 '22 16:06 ziadsarour