0

I am using a NSTimer to control CLLocation services. When the location is updated, I pause the location update service and set up a timer in 60 sec to restart updating the location.

The problem is, sometimes the restart function is called but sometimes it is not. I want to check if the timer is running properly everytime. So is there a way to show in NSLog or any other place that a timer is currently running or not? Or even show the countdown of the timer?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Amy L.
  • 210
  • 2
  • 4
  • 14
  • You could call API and check – vivek bhoraniya Jun 09 '17 at 17:12
  • @vivek could you expand a little on it? Thank you! If you mean [retriving timer info] on the official doc, the properties doesn't show if the timer is counting, and because there is nothing else running in the code except the going-to-be-called function, I have nowhere to call anything either. – Amy L. Jun 09 '17 at 17:35
  • If you keep a weak reference to the timer, you can check to see if that reference is `nil` or not. If you have a strong reference to the timer, you can check to see whether it `isValid` or not. – Rob Jun 09 '17 at 17:37
  • By the way, you're not talking background location updates are you (i.e. after the user has left the app and has gone to the home screen or another app)? That's a completely different kettle of fish altogether. – Rob Jun 09 '17 at 17:38
  • @Rob I am talking about background location updates actually. I know that people say the NSTimer doesn't work in background mode, but it is working sometimes for me. And I am sure the timer is valid because the timer sometimes resumes itself after a long time, I just want to know if the timer is paused by the system. – Amy L. Jun 09 '17 at 18:00
  • The timer isn't being paused; your app, itself, is being suspended (and eventually terminated). When you run it thru the debugger, it will stay running, but in the absence of that, it will stop running. Unless your app has requested the battery-crushing background navigation abilities (something Apple only grants where it's critical to the operation of the app, not just a "I'd like to know"), your timer will not continue to operate. – Rob Jun 09 '17 at 18:18
  • The suggested compromise is the significant change location service which is a little more efficient, though (a) less accurate; and (b) is initiated when the user moves, not after amount of time has elapsed. Possible duplicate of https://stackoverflow.com/questions/26269387. – Rob Jun 09 '17 at 18:20
  • @Rob I was going to ask another question because it is related to this but focusing on different problems. But since you mentioned it, then: my timer is stopped only when the phone is sleeping(screen is black) & not charging, but as long as the screen is light up, no matter what I am doing (staring at the lock screen, using other apps etc), or if the phone is being charged, the timer will fire even if the app is in the background, at least for hours. – Amy L. Jun 09 '17 at 18:27
  • How do you know that the timer is firing? Are you watching the console in Xcode? I.e., what I'm really asking, is the app connected to the debugger when you experience this behavior? – Rob Jun 09 '17 at 18:29
  • I am testing the app in TestFlight, so I am using BugFender to generate remote log file. I see the NSLog messages only if the timer fires and the function is called. The phone is not connected to the computer. And actually if the phone is connected to the computer or charger, the timer will fire on time at all times. – Amy L. Jun 09 '17 at 18:42
  • Does the app have any background modes enabled? – Rob Jun 09 '17 at 19:55
  • @Rob, Yes, location updates, background fetch and remote notifications are all enabled. – Amy L. Jun 09 '17 at 20:33
  • Yep, these can keep your app running in the background (i.e. Keeping your timers alive), but is not guaranteed to do so (if your app was terminated it will restart it as needed). If you add logging statements in the appropriate app delegate calls, you can monitor the app’s lifecycle. I suspect the problem is not the timers themselves, but rather that the OS decided at some point that your app could safely be terminated at some point. – Rob Jun 09 '17 at 20:43
  • @Rob thank you for your answer, maybe I should add more logs and see – Amy L. Jun 09 '17 at 21:14

0 Answers0