Why does my iOS Live Activity countdown widget crash when its ActivityStatus changes to stale?

4 days ago 3
ARTICLE AD BOX

I am creating a Live Activity widget that counts down to a future event. When starting the Live Activity, I set its staleDate to be equal to the date of the future event. This allows me to update the UI of the widget even if my app is not in the foreground when the event's time passes.

Here is a simplified version of the View I use in the Live Activity for the lock screen:

VStack { VStack { Text("The event will happen in") Text(timerInterval: Date.now...context.state.event.date, countsDown: true) } .foregroundStyle(!context.isStale ? .primary : .secondary) if context.isStale { Text("The event is over!") .foregroundStyle(Color.red) } }

My problem is that the widget crashes when the event date (and thus the staleDate) arrives. Instead of updating its UI, the lock screen widget dims and a progress view appears over it and spins endlessly.

If I replace the Text(timerInterval: ClosedRange<Date>, pauseTime: Date?, countsDown: Bool, showsHours: Bool) in the widget with Text(_ date: Date, style: Text.DateStyle), the crash does not occur and the widget UI updates correctly when its ActivityStatus changes to ActivityState.stale. However, Text(date, style: .timer) will begin to count up once the date has passed, and I don't want this behavior. I want the countdown to stop at "0:00", which is the behavior of Text(timerInterval:pauseTime:countsDown:showsHours:).

How can I avoid this crash while still having the behavior of Text(timerInterval:pauseTime:countsDown:showsHours:)?

Read Entire Article