Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #1006

Open
Bahrom2101 opened this issue Jun 22, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Bahrom2101
Copy link

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 Bahrom2101 added the enhancement New feature or request label Jun 22, 2022
@Bahrom2101 Bahrom2101 changed the title I've been working with the Hive Database in an 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. 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. Jun 22, 2022
@ziadsarour
Copy link

ziadsarour commented Jun 26, 2022

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 dart-lang/language#124 (comment) 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants