flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

Invoking background task from Swift directly

Open walsha2 opened this issue 3 years ago • 4 comments

Flutter Code

In Flutter, we have the following example


void taskDispatcher() {
  Workmanager().executeTask((String taskName, Map<String, dynamic> data) async {
  // Do something
  return true;
  });
}

Future<void> main() async {
  ...
  await Workmanager().initialize(
    taskDispatcher,
    isInDebugMode: AppState.debug,
  );
  ...
}

This works great and can be executed from flutter and as a background task (using workmanager.background.task) without any issues. Great.

Swift Code

In digging through the platform channel methods, I was not quite able to discern how to run the executeTask() callback code directly from Swift. I tried various channel method invocations but I could not get the task to run when called directly from Swift. Here is an example of one of the things I tried:

let rootViewController : FlutterViewController = window?.rootViewController as! FlutterViewController
let workMethodChannel = FlutterMethodChannel.init(name: "be.tramckrijte.workmanager/background_channel_work_manager", binaryMessenger: rootViewController as! FlutterBinaryMessenger)
workMethodChannel.invokeMethod("backgroundChannelInitialized", arguments: nil)
workMethodChannel.invokeMethod("onResultSend", arguments: nil)

Can anyone provide a proper example? Neither of those invokeMethod() calls did anything.

@ened do you have any insight? Just asking you because you had done some of the most recent plugin work on the iOS side. Any help is appreciated! I just want to be able to invoke the work_manager registered tasks defined in Flutter, directly from Swift.

walsha2 avatar Jul 15 '21 19:07 walsha2

@walsha2 what is your use case? Do you just want to trigger the background operation?

ened avatar Jul 15 '21 20:07 ened

@ened yup! I just want to trigger it through swift directly, without needing to go through the iOS task scheduler. I thought it was as simple as using the platform channel method calls, but it seems like that was not doing anything.

Was my code snippet above correct or was there something else I needed to do/initialize to invoke it from Swift?

walsha2 avatar Jul 15 '21 21:07 walsha2

@ened yup! I just want to trigger it through swift directly, without needing to go through the iOS task scheduler. I thought it was as simple as using the platform channel method calls, but it seems like that was not doing anything.

Was my code snippet above correct or was there something else I needed to do/initialize to invoke it from Swift?

OK, understood. I'm not sure about the code snippet yet, but I would imagine that the plugin could export a public utility class which does all the leg work for you.

You should not rely on the FlutterMethodChannel and the names as those could change.

Will have a think, #295 should probably come first. :)

ened avatar Jul 15 '21 21:07 ened

@ened sounds good! Yes I came to that realization as well, but I was trying to just get it to work as a proof of concept. I was actually trying to use some of these constant values in the SwiftWorkmanagerPlugin class but they were private hah. So yes, a public/static method does sound like a reasonable solution and worthwhile functionality.

Although... I still do not understand why it is not working. I thought I was close! :)

walsha2 avatar Jul 15 '21 22:07 walsha2