Post not yet marked as solved
Post marked as unsolved with 5 replies, 2,089 views
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:
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.")
		}
}