hive icon indicating copy to clipboard operation
hive copied to clipboard

How to use Hive with Workmanger?

Open shinewanna opened this issue 5 years ago • 10 comments

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]

shinewanna avatar Sep 24 '20 15:09 shinewanna

This line looks wrong. What is it supposed to do?

await Hive.box(MESSAGE_BOX).(messageOb);

simc avatar Sep 25 '20 18:09 simc

That's add bro typo when posting on github issuse. I just add a object on hive box

shinewanna avatar Oct 03 '20 11:10 shinewanna

Same problem here.

Adasqu avatar Oct 21 '20 10:10 Adasqu

@shinewanna what errors do you have. It should work! if the box is open and registered.

muarachmann avatar Oct 21 '20 14:10 muarachmann

@muarachmann Really??, if the app is not in background, it can't open.

shinewanna avatar Oct 24 '20 04:10 shinewanna

@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?

muarachmann avatar Oct 24 '20 07:10 muarachmann

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.

no-response[bot] avatar Nov 07 '20 16:11 no-response[bot]

@shinewanna Hellow, If you got any answers, Will You not Share them with us? hmm..

shaadart avatar May 26 '21 10:05 shaadart

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.

shinewanna avatar Jun 26 '21 12:06 shinewanna

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?

ShafiMunshi avatar Aug 12 '24 17:08 ShafiMunshi