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

Fix: iOS background worker #244

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

armandsLa
Copy link
Contributor

@armandsLa armandsLa commented Mar 29, 2024

There is an issue with how HomeWidgetBackgroundWorker uses async/await in its iOS implementation. Currently, the run method returns immediately, before the platform channel can be initialized and the Flutter callback performed.

I've refactored the HomeWidgetBackgroundWorker with a proper async/await implementation. Now, the run method will return only after the interactivity callback in Flutter has been executed.

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (365f8f9) to head (5cbf63a).

Current head 5cbf63a differs from pull request most recent head effc06b

Please upload reports for the commit effc06b to get more accurate results.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #244   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          103       103           
=========================================
  Hits           103       103           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@armandsLa
Copy link
Contributor Author

I've been testing this PR over the weekend and can confirm that it resolves the issue where widgets might not reload with new data after saveWidgetData and updateWidget are called from the interactivity callback. This issue only occurred when the interactivity callback took a bit longer to execute.

To better understand the issue, the sequence of actions was as follows:

  1. Call HomeWidgetBackgroundWorker.run from an iOS widget.
  2. Receive the interactivity callback in Flutter.
  3. Execute a task that takes longer to complete.
  4. Call saveWidgetData with the new data.
  5. Immediately call updateWidget thereafter.

In this scenario, the widget would reload, but with the previously saved data, not the one saved in step 4. I still have no idea why this is happening. My best guess is that returning .result() from an Intent before data gets saved halts the internal synchronization of user defaults.

@Oleksandr32
Copy link

hi @armandsLa, I have tried your solution and it works.
But now I see this warning:

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.

@armandsLa
Copy link
Contributor Author

@Oleksandr32 Thank you for reporting this issue. I was calling HomeWidgetBackgroundWorker from the main thread, so I never encountered this error. I have now added Dispatch to ensure it is safe to call HomeWidgetBackgroundWorker from any thread. Please let me know if this resolves the issue.

@Oleksandr32
Copy link

@armandsLa The issue has been resolved. Thanks

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

Successfully merging this pull request may close these issues.

None yet

2 participants