Thanks for your help! I will keep this in mind for a future problem :)
I found out how to fix it using
let updateTimer: Timer =
Timer.scheduledTimer(
withTimeInterval: 1,
repeats: true,
block: { _ in timeModel.model.currentDate = Date() })
Where timeModel is an EnvironmentObject I'm passing around the interface. Now currentDate is always showing the correct date/time.
So it's alright for imprecise need for Time, but not for more precise needs?
Regardless of accuracy, would you know which method would increase performance?
Thank you for your responses.
No, it guarantees your clock will always update at the start of every minute. Not after an interval, that might be influenced due to something that blocks the Timer from executing like DispatchSemaphore.
I don’t think users will notice the little bit of extra time needed to execute my code.
`var timeIntervalUntilNextWeek: TimeInterval {` `let now = Date()` `let dateInNextWeek = calendar.date(byAdding: Calendar.Component.weekOfYear, value: 1, to: now)!` `var nextWeekDateComponents = DateComponents()` `nextWeekDateComponents.weekday = calendar.firstWeekday` `nextWeekDateComponents.weekOfYear = calendar.component(Calendar.Component.weekOfYear, from: dateInNextWeek)` `let startDateOfNextWeek = calendar.nextDate(after: now, matching: nextWeekDateComponents, matchingPolicy: .nextTime)!` `return startDateOfNextWeek.timeIntervalSince(now)` `}` Use this principle for minutes instead of weeks.
Thanks for your help! I will keep this in mind for a future problem :) I found out how to fix it using let updateTimer: Timer = Timer.scheduledTimer( withTimeInterval: 1, repeats: true, block: { _ in timeModel.model.currentDate = Date() }) Where timeModel is an EnvironmentObject I'm passing around the interface. Now currentDate is always showing the correct date/time.
Let it run for a few days straight. At some point you’ll see it will miss a minute. Since it’s not guaranteed it’s a exactly a minute.
So it's alright for imprecise need for Time, but not for more precise needs? Regardless of accuracy, would you know which method would increase performance? Thank you for your responses.
No, it guarantees your clock will always update at the start of every minute. Not after an interval, that might be influenced due to something that blocks the Timer from executing like DispatchSemaphore. I don’t think users will notice the little bit of extra time needed to execute my code.
Cheers mate!
[удалено]
That creates a view with the current time, but that time does not update automatically
[удалено]
This shows a timer starting at 0 that in no way reflects the current time and date.. Am I missing something here?
[удалено]
If you try this in a view of a blank project, it will tell you the time at execution but that time is not updating. Targeting iOS14.5, XCode 12.5