How to use a policy for Static Widgets that refreshes daily

I want help in giving a refreshing policy that refreshes once a day.
Answered by Systems Engineer in 615130022
Hi,

To do this, you may pass a TimelineReloadPolicy which explicitly asks to be updated tomorrow. Such as

Code Block swift
/* Return timelines and set policy to reload tomorrow */
func timeline(with context: Context, completion: @escaping (Timeline<MyEntryType>) -> ()) {
...
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())!
let timeline = Timeline<MyEntryType>(entries: entries, policy: .after(tomorrow))
...
}


A similar example is described in TimelineProvider's documentation.
Accepted Answer
Hi,

To do this, you may pass a TimelineReloadPolicy which explicitly asks to be updated tomorrow. Such as

Code Block swift
/* Return timelines and set policy to reload tomorrow */
func timeline(with context: Context, completion: @escaping (Timeline<MyEntryType>) -> ()) {
...
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())!
let timeline = Timeline<MyEntryType>(entries: entries, policy: .after(tomorrow))
...
}


A similar example is described in TimelineProvider's documentation.
Thanks A Lot 😃
I’ve seen an strange behavior with te refresh policy.

if you put a print(“hello”) in the timeline function, it will print a bunch of hello per second, and the CPU will be over 50%.

I would expect it to print hello tomorrow, but it’s no happening.

maybe it’s a bug?

Does it happens to you?



@JugaNet happens for me as well which is a really big problem as I'm calling an API :/ The Timeline seems to ignore the policy, perhaps i'm missing something? Here's my code at least, is this happening for anyone else?
Code Block swift
func timeline(with context: Context, completion: @escaping (Timeline<ChannelEntry>) -> ()) {
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
guard let channels = try? JSONDecoder().decode([Channel].self, from: channelsData) else { return }
update_channel(channel, token) { updatedChannel in
let entry = ChannelEntry(date: currentDate, channel: channels[0])
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}
}


Sorry you ran into this. This is a known issue where if you have the debugger attached to the widget process, the timeline is excessively reloaded. Please keep an eye on future betas where this will get fixed.


Sorry you ran into this. This is a known issue where if you have the debugger attached to the widget process, the timeline is excessively reloaded. Please keep an eye on future betas where this will get fixed.

I use Xcode 12.0 beta 4 and still have the same problem.
Any news?
so we at 14.3 and it's still buggy, how are we suppose to develop like this?
Timeline getting called multiple times, I'm using the policy: .after(/* 1 day */)) and it's not working

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:

on my real calls, im calling to an api and and have a background process downloading 3 images into memory, when this happens its calling the api all those times and running the download image process all of those times, and of course it crashes at a point cuz of memory going after 30MB -- Xcode 15 --

    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
How to use a policy for Static Widgets that refreshes daily
 
 
Q