Reputation: 400
I want my Widget to update for example every 5 seconds. I don't know why it is not working. The code should be right.
Updated Code:
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
let currentDate = Date()
for _ in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .minute, value: 60, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration, clubname: networkManager.clubName)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
Upvotes: 1
Views: 2074
Reputation: 43
You are adding a timeline entry for the same date every time:
let currentDate = Date() // currentDate = now
for _ in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .minute, value: 60, to: currentDate)! // entryDate is ALWAYS currentDate + 60 minutes
let entry = SimpleEntry(date: entryDate, configuration: configuration, clubname: networkManager.clubName)
entries.append(entry)
}
Instead, use a var and update that:
var entryDate = Date()
for _ in 0 ..< 5 {
entryDate = Calendar.current.date(byAdding: .minute, value: 60, to: entryDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration, clubname: networkManager.clubName)
entries.append(entry)
}
Upvotes: 0
Reputation: 392
Because you added the same entry. If you change the value like the below example, it will be refreshed.
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()
let chapter = getChapterWith(level: "1") // it is returned a array.
for index in 0 ..< chapter.count {
let entryDate = Calendar.current.date(byAdding: .second, value: index, to: currentDate)!
let entry = SimpleEntry(date: entryDate, chapter: chapter[index])
entries.append(entry)
}
let timelineDate = Calendar.current.date(byAdding: .second, value: 1, to: Date())!
let timeline = Timeline<SimpleEntry>(entries: entries, policy: .after(timelineDate))
completion(timeline)
}
func getChapterWith(level: String) -> [Chapter] {
return []
}
//Chapter is a struct, it is a variable in SimpleEntry.
struct SimpleEntry: TimelineEntry {
let date: Date
let chapter: Chapter
}
Upvotes: 1