hive
hive copied to clipboard
How to use Hive with Workmanger?
Steps to Reproduce When update or add in Hive db with Workmanger, there's no change on db.
Code sample
callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
MessageOb messageOb;
try {
messageOb = MessageOb.fromJson(inputData);
await SmsHandler.sendMessage(messageOb);
} catch (e) {
print("Error Sending");
}
messageOb.messageStatusIndex = 1;
// final appDocDir = await getApplicationDocumentsDirectory();
// Hive.init(appDocDir.path);
// Hive.registerAdapter(MessageObAdapter(), 0);
// await Hive.openBox(MESSAGE_BOX);
await Hive.box(MESSAGE_BOX).(messageOb);
return Future.value(true);
});
}
Version
- Platform: Android
- Flutter version: [e.g. 1.20.3]
- Hive version: [e.g. 1.2.0]
This line looks wrong. What is it supposed to do?
await Hive.box(MESSAGE_BOX).(messageOb);
That's add bro typo when posting on github issuse. I just add a object on hive box
Same problem here.
@shinewanna what errors do you have. It should work! if the box is open and registered.
@muarachmann Really??, if the app is not in background, it can't open.
@muarachmann Really??, if the app is not in background, it can't open.
I don't understand your issue given the title... are you talking about working in background mode or with workmanager?
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.
@shinewanna Hellow, If you got any answers, Will You not Share them with us? hmm..
There's already someone asked, answered on other issue. So, i closed this.
use IsolateNameServer for background
final sendPort = IsolateNameServer.lookupPortByName("backgroundtask");
if (sendPort != null) {
sendPort.send(afterUpdateMessageOb);
}
on Workmanager.execute and catch the send on your HomePage init like
IsolateNameServer.registerPortWithName(_port.sendPort, "backgroundtask");
_port.listen((dynamic data) async {
MessageOb messageOb = data;
messageService.updateMessageWithUuid(messageOb.uuid, messageOb);
});
and for headless service, u can use as i show in question example code, Hive.init(...) and open and do ur stuff.
There's already someone asked, answered on other issue. So, i closed this.
use IsolateNameServer for background
final sendPort = IsolateNameServer.lookupPortByName("backgroundtask"); if (sendPort != null) { sendPort.send(afterUpdateMessageOb); }on Workmanager.execute and catch the send on your HomePage init like
IsolateNameServer.registerPortWithName(_port.sendPort, "backgroundtask"); _port.listen((dynamic data) async { MessageOb messageOb = data; messageService.updateMessageWithUuid(messageOb.uuid, messageOb); });and for headless service, u can use as i show in question example code, Hive.init(...) and open and do ur stuff.
please provide full example, It's not clear where should I send port?