Widgets refreshing continuously, non-stop, not respecting timeline

Hey, I'm giving a first go at the brand new iOS 14 Widgets. I tried setting up a very simple widget (directly taken from both the documentation examples and the default generated project).

Right now my goal is to have a simple widget that :
  • Refreshes every 10 seconds

  • Displays a random number on every refresh

Here's my code :

Code Block swift
struct Provider: TimelineProvider {
  public typealias Entry = SimpleEntry
  public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {
    let entry = SimpleEntry(status: "Populating", date: Date())
    completion(entry)
  }
  public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
    print("Going through timeline.")
    let date = Date()
    let entry = SimpleEntry(status: String(Int.random(in: 0 ..< 10)), date: date)
     
    let nextUpdateDate = Calendar.current.date(byAdding: .minute, value: 15, to: date)!
     
    let timeline = Timeline(
      entries: [entry],
      policy: .after(nextUpdateDate)
    )
     
    completion(timeline)
  }
}
struct SimpleEntry: TimelineEntry {
  public let status: String
  public let date: Date
}
struct PlaceholderView : View {
  var body: some View {
    Text("Placeholder View")
  }
}
struct WidgetAdventureWidgetExtensionEntryView : View {
  var entry: Provider.Entry
  var body: some View {
    Text(entry.status)
  }
}


In this example I'm trying to have a refresh every 15 minutes, but when ran on simulator, the widget seems to update continuously and random numbers keep getting re-drawn. Like almost 8-9 per second. My print line gets displayed non stop too.

Any idea what I'm doing wrong ?
If I'm reading this correctly you're passing in Date() for all of your entries. My understanding of the API is that you need to project into the future what the values will be by incrementing your Date() for each of the next 14 minutes. I get that you're setting nextUpdateDate, but again I understood this to be that it's reading your date on the .end of the individual entries as the date it needs to refresh.
@rodericthumbworks You mean passing nextUpdateDate to the Entry ? I tried doing so, and my print still gets printed out constantly, and my widget stays blank :/ Or maybe I didn't understand ?
I am experiencing the same behaviour. I set the nextUpdateDate by adding 1 hour, timeline still gets called continuously
Found this - https://developer.apple.com/forums/thread/650724

Looks like debugger might be the issue! I am currently downloading the latest beta, Xcode 12 Beta 4 and see if this works
Everything looks correct in your code. It should be working as you expect. A single timeline entry is generated with a single random number and then after 15 minutes your timeline should be regenerated.

As you update to beta 4, be aware that several of the things you have copied are now deprecated and I have found it acts a little strange with the old methods. The placeholder logic is now handled in a placeholder(in:) alongside snapshot and timeline, which have respectively been changed to getSnapshot(with:in:completion:) and getTimeline(with:in:completion:)
I am pretty much using the same code as above, but I am trying to update my widget once every 3 seconds. But there seems to be minimum period of at least 5 minutes? The simulator always shows that the next refresh is in 5 minutes.

This is my code:

Code Block swift
func getTimeline(in context: Context, completion: @escaping (Timeline<WidgetModel>) -> Void)
{
    let controller = BatteryController()
    controller.refresh()
    let nextRefresh = Calendar.current.date(byAdding: .second, value: 3, to: Date.current)!
let model = WidgetModel(date: nextRefresh, batteries: controller.batteries)
let timeline = Timeline(entries: [model], policy: .atEnd)
completion(timeline)
}


Is this true that the minimum refresh interval is 5 minutes?

Found this - https://developer.apple.com/forums/thread/650724

Looks like debugger might be the issue! I am currently downloading the latest beta, Xcode 12 Beta 4 and see if this works

Bug was fixed in beta 4 or not?
Very simple!

just put a timer in your getTimeline :

Code Block
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
let entryDate = Calendar.current.date(byAdding: .second, value: 1 , to: Date())!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .after(entryDate))
var timer = Timer()
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
WidgetCenter.shared.reloadAllTimelines()
completion(timeline)
}


@Dinejahani
Your solution work fine only if you start your app from xcode and when xcode is debugging. When stop xcode debug the refresh timer stop too
Widgets refreshing continuously, non-stop, not respecting timeline
 
 
Q