fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
826 stars 247 forks source link

[question] callDispatcher runs independently #486

Open ralfeus opened 1 year ago

ralfeus commented 1 year ago

Hi, I use Workmanager to run a background independent isolate. The setup is like this:

/// Entry point to background isolate
@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask(backgroundMain);
}

/// Starts background isolate to host background parts of components
/// This function is executed in foreground and ensures proper start of background
/// isolate
Future<DuplexPort> initAppBackground(String logFile) async {
  Logger.root.info("Starting background isolate");
  Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
  await Workmanager().registerOneOffTask("task-identifier", "app",
      inputData: {'logFile': logFile});

  final port = await _initPort('app-to-background', 'app-from-background');
  return port;
}

/// Main function executed in the background
Future<bool> backgroundMain(String task, JSON? inputData) async {
  ...
}

I've noticed the callbackDispatcher runs even before Workmanager().initialize() is called and as a consequence inputData in backgroundMain is just null. Is it normal behavior?