I have widgets providing their timeline using the .atEnd
reload policy, i.e.:
// AppIntentTimelineProvider:
return Timeline(entries: entries, policy: .atEnd)
// TimelineProvider
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
I can't seem to find any information on what happens after the end of the timeline. So, let's say I've got two days worth of entries, the dev docs for the reload policy say, "A policy that specifies that WidgetKit requests a new timeline after the last date in a timeline passes."
Great! But how does it request the new timeline? Does iOS launch my app in the background and simply re-run the timeline to generate another two days worth of entries? I doubt it.
I figure I need to implement some sort of background task, and the dev docs say how to do it with an Operation
, but then I read that this is an old way of doing it? I've found some info online saying to use something like this, so this is what I've implemented:
let kBackgroundWidgetRefreshTask = "my.refresh.task.identifier" // This has been registered in the info.plist correctly
class SchedulingService {
static let shared = SchedulingService()
func registerBackgroundTasks() {
let isRegistered = BGTaskScheduler.shared.register(forTaskWithIdentifier: kBackgroundWidgetRefreshTask, using: nil) { task in
print("Background task is executing: \(task.identifier)") // This does print "true"
self.handleWidgetRefresh(task: task as! BGAppRefreshTask)
}
print("Is the background task registered? \(isRegistered)")
}
func scheduleWidgetRefresh() {
let request = BGAppRefreshTaskRequest(identifier: kBackgroundWidgetRefreshTask)
// Fetch no earlier than 1 hour from now - test, will be two days
request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 60)
do {
try BGTaskScheduler.shared.submit(request)
print("Scheduled widget refresh for one hour from now")
} catch {
print("Could not schedule widget refresh: \(error)")
}
}
private func handleWidgetRefresh(task: BGAppRefreshTask) {
// Schedule a new refresh task
scheduleWidgetRefresh()
// Start refresh of the widget data
let refreshTask = Task {
do {
print("Going to refresh widgets")
try await self.backgroundRefreshWidgets()
task.setTaskCompleted(success: true)
} catch {
print("Could not refresh widgets: \(error)")
task.setTaskCompleted(success: false)
}
}
// Provide the background task with an expiration handler that cancels the operation
task.expirationHandler = {
refreshTask.cancel()
}
}
func backgroundRefreshWidgets() async throws {
print("backgroundRefreshWidgets() called")
definitelyRefreshWidgets()
}
}
As I've commented above, the line print("Background task is executing: \(task.identifier)")
does print true
so the task has been registered correctly.
I've put the app into the background and left it for hours and nothing is printed to the console. I've implemented a logger that writes to a file in the app container, but that doesn't get anything either.
So, is there something I'm misunderstanding? Should I change the reload policy to .after(date)
? But what makes the timeline reload?
As a second but linked issue, my widgets have countdown timers on them and the entire timeline shows that every entry is correct, but the widgets on the Home Screen simply fail to refresh correctly.
For example, with timeline entries for every hour for the next two days from 6pm today (so, 7pm, 8pm...) every entry in the preview in Xcode shows the right countdown timer. However, if you put the widget on the Home Screen, after about five hours the timer shows 25:12:34 (for example).
No entry in the timeline preview ever shows more than 24 hours because the entires are every hour, and the one that shows a timer starting at 23:00:00 should never get to 24:00:00 as the next entry would kick in from 0:00:00, so it should never show more than 23:59:59 on the timer. It's like the 23:00:00 timer is just left to run for hours instead of being replaced by the next entry.
It's as though the widget isn't refreshing correctly and entries aren't loaded? Given this is the Simulator - and my development device - and both are set to Developer Mode so widget refresh budgets aren't an issue, why is this happening? How do you get widgets to refresh properly? The dev docs are not very helpful (neither is the Backyard Birds example Apple keep pushing).
Thanks!
I've extensivley tested this over the past two days. I tried timelines spanning one day, with:
- 72 entries, every 20 minutes
- 36 entries, every 40 minutes
- 24 entries, every hour
- 12 entries, every 2 hours
- 6 entries, every 4 hours
- 3 entries, every 8 hours
- 2 entries, every 12 hours
In every case, the policy is .atEnd
, and in every case the timeline is not refreshed when it's supposed to be, which causes the widget to display the wrong timer values.
Due to the inflexibility of Text(date, style: .timer)
, I need to write code that calculates the correct timer value but shows less than a day. For example, if it's 11am today and the event date is tomorrow at noon, there are 25 hours between the two dates and the standard .timer
would show 25:00:00, which is not what I want. So, I calculate the next occurrence of noon, and the widget shows "1 day" with a timer of "1:00:00".
Every timeline with a refresh policy of .atEnd
caues the countdown timer to show the wrong values.
I've got it logging to a file, and I can see that the countdown dates are completely correct in every case. The widget is never refreshed at the right time, so the timers always show the wrong values.
The only way I've managed to get this to work is to have a timeline with one entry, and a refresh policy of .after()
, i.e.
let date = Date()
let refreshDate = Calendar.autoupdatingCurrent.date(byAdding: .minute, value: 30, to: date)!
Timeline(entries: entries, policy: .after(refreshDate))
There is clearly something wrong with .atEnd
.