GCD not respecting time intervals of dispatch items to queue

i am having difficulty getting GCD to fire my dispatch at the correct times. It seems to miss by huge margins and i don't understand why it's happening.

i send 3 dispatches to my queue for execution . .one at 5 mins in the future ( 300 seconds ) one at 10 minutes ( 600 seconds ) and another at 20 minutes (1200 seconds)..If my device ( iphone or ipad ) is under charge the three times will fire relativlely close to requested time and my audio file ( 20 seconds ) will play ..

The times are a bit off requested - say within 10-20 percent ..but the audio files do play


If my device is not under charge the times miss by quite alot - sometimes of the three items dispatched - two will be lost - just disappear.


When not charging the times miss by huge margins .. 200 percent and more sometimes

When running the app while charging attached to my Mac or charging from a wall socket charger ..

Timer fire TIMES are much more accurate ! ..

indeed what does that even mean ?


at the top in my viewcontroller i have this code bit ..


var myAudio = AVAudioPlayer()



Then i create my serial queue with the following in my viewcontroller ..


let Myqueue = DispatchQueue(label: "Myqueue")


Then on a button click the user creates 3 different dispatches using this template for each ..


Myqueue.asyncAfter (wallDeadline:DispatchWallTime.now() + 300.0, qos: .userInitiated, flags: DispatchWorkItemFlags(rawValue: 0)) {

print(Date())

self.myAudio.prepareToPlay()

self.myAudio.play()

}


The 3 dispatches have different time intervals . .one at 300 seconds ( 5 minutes ) one at 10 minutes ( 600 ) and another at 20 minutes ( 1200 seconds), each play an audio file in the device bundle .. using AVaudioplayer

i then lock the device . .

i have the key "application_does_not_run_in_background" set to true in my plist info file so my app does not go into background

once the device is locked.


why are my times so far off when the device is not under charge ? ? -

this is same on both my ipad and iphone ..

Even when under charge there is a large skew from the requested times .. making it difficult to use GCD as an accurate replacment timer..

Both my devices are running 9.3.5 as test bed ..

i am using swift compiling for IOS 8.00 and up .. running Xcode 9 on a MacPro 15 inch 2017 ..


Thanks if someone knows why this is so .. i am just not getting it . .

Thanks