WidgetKit TimelineProvider calling timeline() continuously

A newly created Widget extension, with the default code created by Xcode will continuously call the timeline() function while the widget is being debugged.

Code provided below:
Code Block swift
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
public typealias Entry = SimpleEntry
public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {
NSLog("getting snapshot")
let entry = SimpleEntry(date: Date())
completion(entry)
}
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
NSLog("getting timeline")
var entries: [SimpleEntry] = []
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate)
entries.append(entry)
}
NSLog("\(entries)")
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
public let date: Date
}
struct PlaceholderView : View {
var body: some View {
Text("Placeholder View")
}
}
struct SampleWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Text(entry.date, style: .time)
}
}
}
@main
struct SampleWidget: Widget {
private let kind: String = "SampleWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
SampleWidgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}


Can you provide more information and perhaps file a feedback to WidgetKit? A feedback will include helpful logs to help us see what is going on with your device.

From what I see, this may be expected behavior if you are using Xcode Previews to Preview your widget. From your code, it appears you are.
I see the same behavior.

Timeline:

Code Block    
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []
        let currentDate = Date()
        for minuteOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .minute, value: minuteOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate)
            entries.append(entry)
        }
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }


Simple entry func:

Code Block
struct SimpleEntry: TimelineEntry {
        public let date: Date
}



Even for this simple timeline, the CPU of the device goes between 30% to 50% all the time, and that's because, timeline its executing the code inside 17 times per second.

I think it's a bug, or if someone thins otherwise, please share an example.

Thanks in advance,
Looks like this is a known issue. https://developer.apple.com/forums/thread/650724
Happening for me as well when I use the date or reload at end policies. The only one that doesn't refresh constantly multiple times per second is the never policy.

im having the same issue now , the getTimeline is being called multiple times, can't figure out what's going on.

plain simple entry here:

    let currentDate = Date()
    let updateDate = Calendar.current.date(byAdding: .hour, value: 5, to: currentDate)!
    print("NEW Current Date:\(currentDate) update Date is: \(updateDate)")
    let entry = SimpleEntry(date: Date(),configuration: ConfigurationIntent(), items: DummyData.data)
    
    let timeline = Timeline(entries: [entry], policy: .after(updateDate))
    
    completion(timeline)

-- DEBUG console , running real device --

NEW Current Date:2023-10-18 13:58:34 +0000 update Date is: 2023-10-18 18:58:34 +0000
NEW Current Date:2023-10-18 13:58:37 +0000 update Date is: 2023-10-18 18:58:37 +0000
NEW Current Date:2023-10-18 13:58:38 +0000 update Date is: 2023-10-18 18:58:38 +0000
NEW Current Date:2023-10-18 13:58:40 +0000 update Date is: 2023-10-18 18:58:40 +0000
NEW Current Date:2023-10-18 13:58:40 +0000 update Date is: 2023-10-18 18:58:40 +0000`
WidgetKit TimelineProvider calling timeline() continuously
 
 
Q