Read from box from multiple isolates
Is your feature request related to a problem? Please describe. I have audio playing on a separate isolate via audio_service, distinct from the UI. Even when the app is closed, audio can still play. I need to keep track of where the user is holding in the audio, for
1, continuing from that place next time he starts up the app (even if he closes the background play back). 2. UI things - when user navigates to a file, he should see where he's holding even before play back starts, and even if another class is playing.
Saving the current position has to be done in the background audio isolate, because the UI thread could be closed at any time.
What comes out is that the background thread needs read and write access to the box, and the UI thread needs read access.
Describe the solution you'd like I would like for there to be a way to open a box read only, and such an open would be allowed from multiple threads.
Describe alternatives you've considered Currently, I'm planning on having 2 boxes, one for each thread 🙈
Version
- Platform: Android
- Flutter version: 1.12
- Hive version: 1.3.0
Saving the current position has to be done in the background audio isolate, because the UI thread could be closed at any time.
Every isolate could be killed at any time.
Unfortunately, it is quite hard to keep boxes in sync when they are open in multiple isolates and I haven't found a good solution yet. It is already on my agenda to support this somehow but I have no idea when or how.
In your case, I think it is easy to implement the communication between the isolates yourself.
Every isolate could be killed at any time.
Yes, but if the audio isolate is killed, it's ok that we can't record the latest progress, because progress has stopped.
Unfortunately, it is quite hard to keep boxes in sync
I hear that. It would be enough for my use case just to be able to read the contents of the box once on app start, even though another isolate is using the box; I wouldn't need them synced after. Is that already supported? Or would that break the "Only one process can access a box at any time" rule?
In your case, I think it is easy to implement the communication between the isolates yourself.
Perhaps. audio_service abstracts the UI and audio isolates, so I'd probably have to make a third background isolate in charge of progress data; I don't know exactly, I haven't worked yet with dart background isolates myself yet.
I'm working with this idea in my project, as I know, we cannot open box in multiple isolates , see #122 or you will see this kind of error
HiveError: This should not happen. (while opening a box)
So, I make a static function that create an instance for box and pass it to isolate function, In your case that should be backgroundTaskEntrypoint . and it works!
I am using workmanager package to do repeating stuff (e.g. syncing data with server) - so I have no access to main isolate.
No idea how to fix this, especially because I write to hive in the background isolate.
I am using
workmanagerpackage to do repeating stuff (e.g. syncing data with server) - so I have no access to main isolate. No idea how to fix this, especially because I write to hive in the background isolate.
My solution is:
- In WorkManager task (=BACKGROUND) reading from Hive is OK, but I have to initialize alwalys here:
await Hive.initFlutter();
objectBox = await Hive.openBox('hive_name');
- Next I send message to MAIN thread during Isolates:
sendPort = IsolateNameServer.lookupPortByName("XXX_CHANNEL_NAME");
...
sendPort.send({'key': key, 'json': hiveObj.toJson()});
I send only key from Hive and JSON (created from Hive instance). In MAIN I listen on the channel, find Hive object from DB and update from JSON and call save().
Conslusion: Read in BACKGROUND, write only in MAIN thread, comunication during Isolates. I don't know if this is the best solution, but it works yet.
Sorry for my English. Z.
My final solution is:
- Reading from the Hive is possible in the background process (BP). Data for init BP is always read from Hive.
- BP can measure independently of the application run. I always save the BP result in separate JSON files. I only use Isolates as one-way BP communication to the main process.
- The main process either gets information from BP via a Isolates or retrieves it from temp JSON files when started. In both cases, it modifies (deletes) the temp JSON files.
Z.
Isolate support has been pre-released for hive_ce!
Get the pre-releases here:
hive_ce hive_ce_flutter hive_ce_generator
Please try out IsolatedHive in your projects and report any issues you may have. I want to make sure all potential issues are ironed out before I make a real release.