Issue with DeviceActivityMonitor - eventDidReachThreshold Callback Not Triggering Properly

Hello,

I'm currently experiencing an issue with the DeviceActivityMonitor extension in my code, specifically with the eventDidReachThreshold callback. I'm hoping to get some insights into why this problem occurs and how to resolve it.

Problem:

Issue 1: The eventDidReachThreshold callback is not triggering as expected. It appears that the callback is not being invoked when the threshold is reached.

Issue 2: After a few seconds, the eventDidReachThreshold callback starts to trigger multiple times. This unexpected behavior is causing problems in my code, as it results in incorrect actions being taken.

iOS version: iOS16.7.2 and iOS17.1

Xcode version: 15.0.1

Swift version: 5.9

Here is my code to start the monitoring:

func startMonitoring() {
        var startTime : DateComponents = DateComponents(hour: 0, minute: 0)
        let endTime : DateComponents = DateComponents(hour: 23, minute: 59)
        
        /// Creates the schedule for the activity, specifying the start and end times, and setting it to repeat.
        let schedule = DeviceActivitySchedule(intervalStart: startTime, intervalEnd: endTime, repeats: true, warningTime: nil)
        /// Defines the event that should trigger the encouragement.
        let event = DeviceActivityEvent(applications: socialActivitySelection.applicationTokens, categories: socialActivitySelection.categoryTokens, webDomains: socialActivitySelection.webDomainTokens, threshold: DateComponents(minute: 2))
        let events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [.socialScreenTimeEvent : event]
        
        do {
            activityCenter.stopMonitoring([.socialScreenTime])
            /// Tries to start monitoring the activity using the specified schedule and events.
            try activityCenter.startMonitoring(.socialScreenTime, during: schedule, events: events)
        } catch {
            /// Prints an error message if the activity could not be started.
            print("Could not start monitoring: \(error)")
        }
    }

If there are any known workarounds or potential solutions, please share them.

Thank you for your help in resolving this problem.

Issue with DeviceActivityMonitor - eventDidReachThreshold Callback Not Triggering Properly
 
 
Q