How does the threshold in DeviceActivityEvent work

Hey everyone,

I'm working on implementing an AppLimit, where after accumulating x minutes of Screen Time for an app, it should be blocked. It works fine on the first day, but stops functioning correctly on subsequent days.

What I'm Doing

  1. I start a 24/7 schedule with a DeviceActivityEvent that has a specified Screen Time threshold.

  2. In my DeviceActivityMonitor, I'm reacting to the eventDidReachThreshold. Once the accumulated time is reached, the app is blocked. This works as expected on the first day.

Issues I'm Experiencing / Questions

  • Second Day Issue: On the second day, the app is no longer blocked after the Screen Time threshold is reached, even though it worked on the first day. This leads me to suspect that a DeviceActivityEvent is "consumable". Is this correct?
  • Pre-existing Screen Time Issue: If a user has already surpassed the Screen Time threshold before monitoring starts, the app isn't blocked once the schedule is set up. This leads to 2 issues:
  1. I would expect that the accumulated amount of time after starting the schedule would result in the call of eventDidReachThreshold. But it is never called
  2. It could also be the case that the previously accumulated time is being kept in mind, but that would mean the apps should be blocked, which isn't the case.

Does the threshold account for accumulated Screen Time before the schedule begins? I haven't tested setting a limit of 10 minutes, accumulating 3 minutes of Screen Time, then starting the schedule and accumulating the remaining time, but I'm curious if anyone has encountered this behavior.

Does anyone have an explanation for this behavior? Any help would be greatly appreciated!

Answered by sajjadalhusseini2 in 801962022

When defining an event, starting from iOS 17.4 you can set includesPastActivity to true. Which I think solves one of the problems you mentioned. try something like this:

 let event = DeviceActivityEvent(
            applications: appTokens,
            threshold: DateComponents(minute: 60)
           includesPastActivity: true
        )
Accepted Answer

When defining an event, starting from iOS 17.4 you can set includesPastActivity to true. Which I think solves one of the problems you mentioned. try something like this:

 let event = DeviceActivityEvent(
            applications: appTokens,
            threshold: DateComponents(minute: 60)
           includesPastActivity: true
        )
How does the threshold in DeviceActivityEvent work
 
 
Q