Reputation: 957
Xcode provides this code to update 5 times an hour:
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
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)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
I tried this but it doesn't update right.:
var entries: [SimpleEntry] = []
let currentDate = Date()
let entryDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
let entry = SimpleEntry(..)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
When I run on my device it updates, but it doesn't feel like it is updated consistently. It is definitely not updating once a day.
Upvotes: 1
Views: 2248
Reputation: 5115
You’ve got two problems:
currentDate
, which is the current date and time, so if this runs at 2:30pm today it would refresh at 2:30pm tomorrow.Give this a try:
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
// Generate a timeline with one entry that refreshes at midnight.
let currentDate = Date()
let startOfDay = Calendar.current.startOfDay(for: currentDate)
let endOfDay = Calendar.current.date(byAdding: .day, value: 1, to: startOfDay)!
let entry = SimpleEntry(date: startOfDay)
let timeline = Timeline(entries: [entry], policy: .after(endOfDay))
completion(timeline)
}
Here I’ve used the Calendar.current.startOfDay()
method to get a Date to use for my entry. Then I add a day to it to get midnight tonight, which I use in the Timeline’s refresh policy.
Upvotes: 3