Background update home widget iOS

The application was initially written in Swift, and we released an update where the app was rewritten in Flutter. Currently, we are adding a widget natively written in SwiftUI to the Home screen. The widget updates are managed by BGTaskScheduler. In BGTaskScheduler, an API request is made to fetch the latest data. The data is then processed to calculate an average value, which is subsequently sent to UserDefaults. The widget displays data fetched from UserDefaults. The minimum update interval is set to 30 minutes.

When testing the widget updates through a build in Xcode, the widget updates as expected at the specified interval. However, when this build was provided to users via TestFlight, the widget does not update for them. Could this issue be related to TestFlight’s resource limitations? Is there any guarantee that releasing this version will ensure the widget updates correctly for users?

Answered by Engineer in 815057022

If and when the BG Task is going to run is at the system's discretion. Setting the minimum interval to 30 minutes is not a command to do it so every 30 minutes, but rather to run it at most once every 30 minutes. The system will throttle the task executions based on the device and app state.

If you haven't done so, you should read iOS Background Execution Limits to understand the factors that go into the decision whether your background task will be executed or not.

If your tasks are working when built with Xcode, that is goo news, as it means your code is correct. The above mentioned throttle is lifted when the app is being debugged for you to see that your code works.

Background Tasks will run on their own schedule, and if this is not adequate for your widget updates, then you may want to investigate other solutions.

The only way you can you can do this by way of sending push notifications every 30 minutes, and then use a UNNotificationServiceExtension to update your widget while processing the push notification. This will not be silent though, and whether this is an acceptable way for your users will be up to you to decide.


Argun Tekant /  DTS Engineer / Core Technologies

If and when the BG Task is going to run is at the system's discretion. Setting the minimum interval to 30 minutes is not a command to do it so every 30 minutes, but rather to run it at most once every 30 minutes. The system will throttle the task executions based on the device and app state.

If you haven't done so, you should read iOS Background Execution Limits to understand the factors that go into the decision whether your background task will be executed or not.

If your tasks are working when built with Xcode, that is goo news, as it means your code is correct. The above mentioned throttle is lifted when the app is being debugged for you to see that your code works.

Background Tasks will run on their own schedule, and if this is not adequate for your widget updates, then you may want to investigate other solutions.

The only way you can you can do this by way of sending push notifications every 30 minutes, and then use a UNNotificationServiceExtension to update your widget while processing the push notification. This will not be silent though, and whether this is an acceptable way for your users will be up to you to decide.


Argun Tekant /  DTS Engineer / Core Technologies

You can check for updates once every 20-30 minutes by using a Widget timeline with .atEnd or .after(Date). If APNs is a large buildout you can also consider a simple CloudKit integration and use a CKSubscription, where your user defaults value is updated in CloudKit and in turn would trigger a notification that would allow you also update your Widget.

Rico


WWDR | DTS | Software Engineer

Background update home widget iOS
 
 
Q