Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await calls not working on iOS HomeWidget.registerInteractivityCallback while in background #176

Open
bypass112 opened this issue Oct 31, 2023 · 3 comments

Comments

@bypass112
Copy link

bypass112 commented Oct 31, 2023

There seems to be an issue on iOS side when clicking the Home Widget button to refresh the data on the Home Screen Widget:

  1. If you try to call a http get request from dart @pragma("vm:entry-point"), while having the app in the background and installed as Released on a physical device the http calls won't finish unless you bring the app manually to the foreground. Requests will work fine as long the app is running through XCode (and debugging) on the device
  2. An alternative would be to call
    Workmanager().executeTask((task, inputData) async { //API CALL HERE return Future.value(true); });
    from the home_widget 'backgroundCallback' set to respond from the home widget button call, but it will fail and the app will just respond with

The 'home_widget/background' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method backgroundChannelInitialized on channel be.tramckrijte.workmanager/background_channel_work_manager)

Is there any way to fetch data from API REST calls using the new method of button clicks on the home widget for iOS?
Android is working fine with the current dart code that I have and is populating the widget UI elements fine from the http requests.

@bypass112
Copy link
Author

It also seems that whatever the background method used for the HomeWidget.registerInteractivityCallback call, you can't add any await in it.
For example any code added after an await Future.delayed(Duration(seconds: 3)); will not be executed, untill after the app is brought to the foreground.
This may not be an issue regarding http calls, but await calls.
Could someone help me find a solution for this please?

@bypass112 bypass112 changed the title Http calls not working on iOS home widget button click await calls not working on iOS home widget button click Oct 31, 2023
@bypass112 bypass112 changed the title await calls not working on iOS home widget button click await calls not working on iOS HomeWidget.registerInteractivityCallback while in background Oct 31, 2023
@mchudy
Copy link
Contributor

mchudy commented Dec 6, 2023

Possible workaround is to start an iOS background task when executing the background callback (e.g. with this library https://pub.dev/packages/ui_background_task). I believe the problem is that the iOS can arbitrarily stop the execution of the Dart code when the app is in the background. Background task gives you 30 seconds to complete the operation.

@bypass112
Copy link
Author

As a temporary solution I just moved the async tasks from Dart side directly into iOS Swift. Doing this seems to properly fetch any data from the await calls.
Generally speaking iOS is very strict with any background tasks that consume resources, so I believe that iOS was killing any attempt that I had to fetch data over http calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants