0

Problem

I need to get a callback when at least X amount of time has passed since the date for the callback has been set.

Example 1: This would have worked great, but it's possible to trigger an execution of the block by setting the date earlier than the correct time right now:

let responseDate = Date().advanced(by: 60) // 1 min
OperationQueue.current.schedule(after: .init(responseDate), {
print("the time is now!") // possible to set the current date 1 min before
}

On the other hand, the solution for getting a current uptime from this answer works great, but it requires timer constantly running to check if we're close to date.

Is it possible to combine these two approaches and somehow "attach" a callback to KERN_BOOTTIME, so that the OS will call my method when the boottime reaches a certain value?

I'm looking as well to alternative engineering solutions that satisfy two criterias:

  1. It should not be possible to trigger the callback by resetting the device date to some arbitrary value in the past
  2. If the device has been put to sleep (e.g. by pressing the on/off side button), the clock should still be "ticking", so that the method will be called back while the app is running in the background.

More details:

  • Backgrounding / app termination is out of scope
  • The main point is to prevent a bypass by switching the date backwards in the settings.
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • Updated my question with more details to clarify the context. The backgrounding (suspended) is not that important, what's more important is to detect the event when the app is running in the foreground and/or after opening the app which has been running in the background But there is no need to invoke the method while in background – Richard Topchii Nov 24 '21 at 14:26
  • FWIW, ignoring the operation queue for a second, I did a few quick GCD timer tests (with “strict” option to avoid timer coalescing). The “deadline” rendition is unaffected by users manually adjusting the time on the device, whereas the “wallDeadline” rendition, as you would expect, was. (E.g. I started 2 timers, 3 minutes each, and the “wallDeadline” fired when the adjusted date/time on the device was three minutes later than the date/time before starting the timer, whereas the “deadline” rendition fired three minutes after it started, regardless of any device date/time adjustments. – Rob Nov 24 '21 at 20:55
  • The concern is that the documentation suggests that “deadline” rendition excludes time elapsed when the device is suspended, though, although in my brief test on physical iOS devices, it kept “ticking” even after I pressed the on/off side button. – Rob Nov 24 '21 at 20:55

0 Answers0