0

I'm working with timer and I've try to make it works in background. On simulator this works fine but on my device (iOS 11) it's very slow: 1 seconde became 5 or 6 secondes...

This is my code for run application in background:

backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(
    expirationHandler:
      {UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)}
    )

Info.plist: Application does not run in background : NO

How can I make it works?

EDIT:

This is my timer code:

timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, 
  selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)

EDIT 2:

func updateTimer () {
    var j = 0
    for _ in rows {
        if (rows[j]["Playing"] as! Bool == true ) {
            rows[j]["time"] = (rows[j]["time"] as! Double + 0.01) as AnyObject
        // print(rows[j]["time"]) - PRINT OUTPUT HERE
            rows[j]["lastTime"] = (rows[j]["lastTime"] as! Double + 0.01) as AnyObject
        }
        if (rows[j]["lastTime"] as! Double > 60.0) {
            min[j] += 1
            rows[j]["lastTime"] = 0.00 as AnyObject
        }
        j += 1
    }
}
pierreafranck
  • 445
  • 5
  • 19
  • could you please also share your timer code here? – AlexWoe89 Oct 09 '17 at 07:35
  • @AlexWoe89 done ;) – pierreafranck Oct 09 '17 at 07:37
  • I assume you are debugging out some values in the ViewController.updateTimer function some prints ... did you test your output also in the console? – AlexWoe89 Oct 09 '17 at 07:51
  • @AlexWoe89 yes i've print my output in console, and it's very very slow when the application run in background. It's only happened on my device and not on simulator... – pierreafranck Oct 09 '17 at 07:58
  • Btw, you are not using 1 second as an interval, you are using 0,01 seconds: https://developer.apple.com/documentation/foundation/timer/1415941-scheduledtimer – Marco Pace Oct 09 '17 at 07:58
  • @MarcoPace Yes, I know! So 0,01 sec become 0,05 sec or more in background. – pierreafranck Oct 09 '17 at 08:02
  • Can you share the code in updateTimer to see how you debug it? – Robert D. Mogos Oct 09 '17 at 08:11
  • @RobertD.Mogos done. – pierreafranck Oct 09 '17 at 08:20
  • Don't do background tasks with Timer. It will not work as you might expect. https://stackoverflow.com/questions/43755587/swift-timer-scheduledtimer-doesnt-work – Nishant Bhindi Oct 09 '17 at 09:37
  • 1
    Background Tasks and Timers started from Background Tasks have limitations. One thing is that `NSTimer` may stop if the app goes to background. See [https://stackoverflow.com/a/42319410/1317117](Swift 3 - How to make timer work in background) or [https://stackoverflow.com/a/5187836/1317117](NSTimer on background IS working). It does not directly answer your question but it gives you some hints why it does not work as expected. A (temporary) suspended timer may lead to your effect. – andih Oct 10 '17 at 03:38

2 Answers2

0

Instead of just printing your output, which might be executed from a different thread, print it from the main thread. That's why you might see the delay:

DispatchQueue.main.async {
  print(rows[j]["time"])
}
Robert D. Mogos
  • 900
  • 7
  • 14
0

Your timer interval is too short. Timers have a resolution of 50-100 ms, so your interval (10 ms) can't get executed so fast. I think when your app is in the background this effect increases.

Take a look at this answer: https://stackoverflow.com/a/30983444/5613280

Martin
  • 151
  • 10
  • Thanks for your asnwer. I've try with a delay of 50ms and same result... very slow in background – pierreafranck Oct 09 '17 at 08:47
  • Did you also try it with one full second? – Martin Oct 09 '17 at 08:54
  • Yes, I just try it, it works. But I need to be precise at 0.01 :'( – pierreafranck Oct 09 '17 at 09:05
  • Take a look at [this](https://stackoverflow.com/questions/20261129/better-alternative-for-nstimer), you would replace the Timer with DispatchAfter – Robert D. Mogos Oct 09 '17 at 09:16
  • I'm afraid that this is not possible, the reason for that is clearly explained in the post i linked above. Can you edit your answer and describe what you want to achieve with the timer? Maybe there is another working solution for this problem. – Martin Oct 09 '17 at 09:16