Setting the TimelineProvider refresh interval for Widget

You shouldn’t rely on the TimelineReloadPolicy – it specifies the earliest date when the Timeline is refreshed, it is not guaranteed that it reloads at that specific time.

From my observations, a Widget is more likely to reload the timeline with the atEnd policy.

A policy that specifies that WidgetKit requests a new timeline
after the last date in a timeline passes.

Here is a possible solution:

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
    print("getTimeline")
    let entries = [
        SimpleEntry(date: Date()),
        SimpleEntry(date: Calendar.current.date(byAdding: .minute, value: 1, to: Date())!),
    ]

    let timeline = Timeline( entries: entries, policy: .atEnd)
    completion(timeline)
}

Note that the last entry may or may not be used. If the timeline is refreshed immediately after the second entry’s date, the second entry won’t be shown. However, sometimes the timeline may be reloaded with a delay – then the second entry will be visible (until the timeline is refreshed).

Leave a Comment