After it expired on February 9, it also expired on February 14.
The failure codes are as follows:
let deviceActivityCenter = DeviceActivityCenter()
let deviceActivityName = DeviceActivityName(
"GoalHabit-" + habit.id.uuidString
)
let startInterval = DateComponents(hour : 0, minute : 0)
let endInterval = DateComponents(hour : 23, minute : 59)
let schedule = DeviceActivitySchedule(
intervalStart: startInterval,
intervalEnd: endInterval,
repeats: true
)
do{
try deviceActivityCenter.startMonitoring(
deviceActivityName,
during: schedule,
events: [deviceActivityEventName : deviceActivityEvent]
)
return true
}catch {
return false
}
deviceActivityEvent is an action to be performed after reaching the threshold
override func eventDidReachThreshold(_ event: DeviceActivityEvent.Name, activity: DeviceActivityName) {
super.eventDidReachThreshold(event, activity: activity)
let viewContext = PersistenceController.shared.container.viewContext
let (year, month, day) = getYearMonthDay(.now)
let newHabitRecord = HabitRecord(context: viewContext)
newHabitRecord.id = UUID()
newHabitRecord.habitId = id
newHabitRecord.year = Int16(year)
newHabitRecord.month = Int16(month)
newHabitRecord.day = Int16(day)
newHabitRecord.duration = middleValue * 60 // 当前思考的秒
newHabitRecord.type = 0
newHabitRecord.createDate = .now
do {
try viewContext.save()
WidgetCenter.shared.reloadAllTimelines()
}catch {
}
}
I wish I knew why it failed。
My system has been upgraded to 18.3。