GCD - dispatch time very inaccurate

i am trying to use the dispatch queues as a timer to run a small block of code

that plays a short audio file..


The timer always misses it's time to fire mark settings by what i consider is a really a lot.

It plays sometimes 10 to 20 minutes later when programmed to fire one hour in the future . .it always fires late ..

the times vary.


The timer is set to fire one hour after the app resigns .. ( locks workstation with app still running in foreground ) ..

The key Application does not run in background in plist info is set to true so the app does not resign into the background.


When the timer fires the small audio file in the bundle plays itself ..


Does someone perhaps know why it misses the timimg mark so badly ?

Is there something i am not doing correctly ?


Timers are more accurate if iphone or ipad runnig the app is plugged in to charge or connected to MacPro ..

less accurate when not charging. No idea why this is either.

I have another relevant thread in this forum trying to use a slightly different approach .. but so far no comments.


Thanks


https://forums.developer.apple.com/thread/91466


    class TrainingViewController: UIViewController {

    var myAudio = AVAudioPlayer()
    var timer: DispatchSourceTimer!




        let queue = DispatchQueue(label: "my.serial.queue", qos: .userInitiated)
   
        timer = DispatchSource.makeTimerSource(flags: .strict, queue: queue)
        timer?.schedule(wallDeadline:DispatchWallTime.now() + 3600, leeway: .seconds(120))

        timer?.setEventHandler { [weak self] in

        self?.myAudio.prepareToPlay()
        self?.myAudio.play()
    }



=======================

other perhaps relavent bits from this view controller ..

..

   override func viewDidLoad() {
        super.viewDidLoad()
        do {
            try AVAudioSession.sharedInstance().setActive(true)
            } catch _ as NSError {
            print("we had error")
            print(NSError()
            }

        do {
            myAudio = try AVAudioPlayer
           (contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "file1", ofType: "m4a")!))
            myAudio.prepareToPlay()
            myAudio.volume = 0.1
            var audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setCategory(AVAudioSessionCategoryPlayback)
                }
            catch {
                print("error")
                  }
            }
            catch {
            print("error")
                  }
   
   
    }
GCD - dispatch time very inaccurate
 
 
Q