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.

Issue 3: There are instances where the eventDidReachThreshold callback provides an incorrect event name.

iOS version: iOS16.7.2 and iOS17.1.1

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)")
        }
    }

In addition, I should mention that, with each iteration through the eventDidReachThreshold callback, I increment the threshold by 2 minutes and restart the monitoring.

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

Thank you.

Replies

I'm not sure why issue1 and issue3 are caused.

But as for issue2:

You mentioned: "In addition, I should mention that, with each iteration through the eventDidReachThreshold callback, I increment the threshold by 2 minutes and restart the monitoring. "

Note that the activity duration is calculated using the given time interval. (00:00 to 23:59 in your case)

So if the the the duration has exceeds the given threshold when you call 'startMonitoring', the eventDidReachThreshold callback will be called immediately.

This may be why you see the eventDidReachThreshold callback got triggered multiple times since you're monitoring recursively.

  • I appreciate your valuable insight regarding issue 2. I've made adjustments to my calculation system, initiating each monitoring restart with the date and the given threshold. This modification has effectively addressed the problem of multiple callbacks being triggered.

    Issue 1 persists, and I'm currently conducting thorough investigations to uncover the root cause and identify an appropriate solution. Your additional ideas or suggestions on this matter would be highly appreciated.

Add a Comment