New Swiftui Text timer not counting down

Hi using this new Text method for timers is not counting down. Here is an example of how I implemented it.

Text(.now, format:.timer(countingDownIn: Date.now..<Date.now.addingTimeInterval(120),
                    showsHours: true,
                    maxFieldCount: 2,
                    maxPrecision: .seconds(60))
            )

After waiting over a minute it never counts down

Answered by Vision Pro Engineer in 790740022

Hi!

Thanks so much for finding that, please submit a bug report at https://feedbackassistant.apple.com and post the FB number here.

In the meantime, use this to do the same thing:

 Text(timerInterval: Date.now...Date(timeInterval: 120, since: .now))

Documentation: https://developer.apple.com/documentation/swiftui/text/init(timerinterval:pausetime:countsdown:showshours:)

Hi!

Thanks so much for finding that, please submit a bug report at https://feedbackassistant.apple.com and post the FB number here.

In the meantime, use this to do the same thing:

 Text(timerInterval: Date.now...Date(timeInterval: 120, since: .now))

Documentation: https://developer.apple.com/documentation/swiftui/text/init(timerinterval:pausetime:countsdown:showshours:)

I am trying to use the following in a widget of mine that sometimes needs to show seconds counting down.

Text(.now, format:.timer(countingDownIn: Date.now..<Date.now.addingTimeInterval(55), showsHours: false, maxFieldCount: 1))

If I sent maxFieldCount to 2 I see minutes and seconds ("55:03") but if I set it to 1 I see "55 seconds" instead. I expected to see something like "03"

Oh, I also see the it's not counting down as previously mentioned.

I was trying to use this instead, but cannot get rid of the minutes so I see values like "00:55"

Text(timerInterval: Date.now...Date(timeInterval: 120, since: .now), countsDown: true, showsHours: false)

I am trying these since my original widget code was using a timeline that would cause an update every second (if seconds needed to be displayed) but that caused flashing especially if the contrast between background and foreground colors is high.

Text(.now, format:.timer(countingDownIn: Date.now..<Date.now.addingTimeInterval(120),
                    showsHours: true,
                    maxFieldCount: 2,
                    maxPrecision: .seconds(60))
            )

Using Date.now here confused me 🤔. The first argument .now will be evaluated when the view body is updated, in a very unpredictable way. The text will stay on the last view update time.

I think this new API should be used in this way:

@State private var date: Date = .now

var body: some View {
    Text(date, format: .timer(...))
        .onReceive(Timer.publish(/* on every seconds */)) {
            date = $0
        }
}

UPDATE:

Found: https://developer.apple.com/documentation/swiftui/timedatasource/currentdate

Simply change Date.now to TimeDataSource<Date>.currentDate.

New Swiftui Text timer not counting down
 
 
Q