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

Merged
merged 5 commits into from
Jul 15, 2024
Merged

Fix: iOS background worker #244

merged 5 commits into from
Jul 15, 2024

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 (1b0456f) to head (af3be43).

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

☔ 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

@armandsLa
Copy link
Contributor Author

@ABausG will you be able to review and merge this PR?

@ABausG
Copy link
Owner

ABausG commented Jul 5, 2024

Yes sorry!! Will do it after FlutterCon

Copy link
Owner

@ABausG ABausG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - had to read a bit about async / await works on Swift. Thanks for this. currently looking into the failing android pipeline, but I should get this landed and released this weekend!

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