DeviceActivityMonitor eventDidReachThreshold triggering every time in same second as intervalDidStart gets called

Hello,

I am trying to make use of Screentime API in my app, I have issue with the DeviceActivityMonitor extension. I have schedule DeviceActivitySchedule which I set like this:

let schedule = DeviceActivitySchedule(
    intervalStart: DateComponents(hour: 00, minute: 00),
    intervalEnd: DateComponents(hour: 23, minute: 59),
    repeats: false
)

and DeviceActivityEvent which I set like this:

let dateComponent = DateComponents(minute: 1)
var events: [DeviceActivityEvent.Name: DeviceActivityEvent] = [
    .encouraged: DeviceActivityEvent(threshold: dateComponent)
]

The issue is that every time I start monitoring, by calling this piece of code from the app:

do {
                    print("Try start monitoring...")
                    try center.startMonitoring(.daily, during: schedule, events: events)
                } catch {
                    print("Error: ", error)
                }

I catch in the extension that the event intervalDidStart is called, but in the same second I get called eventDidReachThreshold. What could be done wrong? Is event set properly? I was trying to set event with different Datecomponents (minute, hour, second), by setting different amount of time, like:

let dateComponent = DateComponents(minute: 3)

let dateComponent = DateComponents(second: 120)

but nothing worked differently, every time, it was triggering eventDidReachThreshold right after the interval starts.

If I put something unnormally high, like:

let dateComponent = DateComponents(second: 120)

The event eventDidReachThreshold is not triggered same time as the intervalStarts, but it is not counting time properly.

Please if someone could help me, I want to be able to use events properly, with the amount of time which I set. Any help would be appreciated.

Thanks in advance

Post not yet marked as solved Up vote post of haris0092 Down vote post of haris0092
1k views

Replies

This means that your event's threshold has already been reached. In other words, the device has been used for 1 minute since the start of the schedule's interval (i.e. the start of the current day). If you want your event's threshold to be computed based on any device usage accumulated from now until the end of the day, you must specify a different start time for the schedule.

  • Kmart, what if day already ended, but eventDidReachThreshold invokes anyway? What if on the previous day, I didn’t open specified apps, but eventDidReachThreshold invokes anyway? Where else could be the problem?

Add a Comment

Same issue!

Kmart, what if day already ended, but eventDidReachThreshold invokes anyway? What if on the previous day, I didn’t open specified apps, but eventDidReachThreshold invokes anyway? Where else could be the problem?

Same issue here! Still no idea how to fix this