Reputation: 879
My problem is that flutter Hive cannot be opened from multiple isolates. So I want the data fetched in workmananger task to be sent to the main isolate where the hive box is opened and modify it there to avoid corrupting it. I should take into consideration when the app is alive and when it is not => for example when the app is not alive, I edit the hive file directly since it will be only opened in the workmanager isolate whereas if the app is not alive I send the data to the main isolate and edit the hive file. My problem is that I do not know how to track lifecycle within the workmanager task and I do not know how to send the data to the main isolate. Any workarounds or solutions for the problem or how to code the above?
Upvotes: 6
Views: 1424
Reputation: 879
After some research the solution is as follows:
First you register a port on the initialization of the main isolate like this:
void listenToUpdatesFromWorkManager(String name) { var port = ReceivePort(); IsolateNameServer.registerPortWithName(port.sendPort, name); }
You give it a unique name in order to identify it from any other opened isolates like this:
SendPort sendPort = IsolateNameServer.lookupPortByName(name);
Upvotes: 2