WatchOS2 - NSTimer in background

Hello,


I'm running into an issue where I cant keep the timer running on background (ie when the watch goes in stand-by etc)


On simulator:

The behaviour is as expect, the timer continues in the background and everything is textbook perfect


On Apple Watch:

This is based on feedback and demonstrations. The timer freezes and continues when app is in foreground.


So I'm wondering why the discrepency and how can I ensure the timer runs when the watch goes to sleep?

Replies

Instead of a timer why not use an NSDate and then check elapsed time with timeIntervalSinceNow method?

I could do many things but NSTimer is the most logical one here. Its how I do it on iOS and it works on Apple Watch if app is in foreground.


On simulator, it even works if watch goes to sleep. So I'm trying to understand two things:


1. Why the discrepency between Apple Watch and Simulator

2. Why doesnt NSTimer work in background on Apple Watch.


If its really the case that it doesnt work in background, I think its stupid that Apple allows its own apps to run in background but not 3rd party apps. Again this is anti-competitive in my opinion.

Watch apps cannot run in background (even Apple ones except Workout).

You can start a HKWorkoutSession in order to keep your app active while collecting data from sensors (like Workout app).

It's not anti-competitive, it's energy saving.

Hello,


I do start a healthKitStore.startWorkoutSession(workoutSession) but the watch still goes to sleep.


And it is anti-competitive, though I see/understand the energy saving initiative, heres what I see wrong:

- You say apps can't run in background, even Apple apps EXCEPT Workout

- My app is also a workout app and I start a workout workoutSession but can't run in background

- App should behave exactly as Apple's workout app but doesnt

- Now my app even if it works will need to stay all during the whole workout

- Users wont be happy with that and will prefer using the Apple workout app

==== Anti-competitive


Maybe I'm understanding something, let me know 🙂


PS I did notice that Healthkit was not linked nor imported in my Watch app extension yet it let me call a session. I wonder if thats the cause why it doesnt work on a real Apple Watch, yet works on Simulator.

What do you mean for "running in background"?

Workout app collects data from heartbeat sensor while in background.

UI updates and other stuff are done foreground.

I mean have my NSTimer run in the background and contiune when it reappears. The timer seems to freeze on peoples devices, though it works on the simulator.

The Simulator behaviour is not perfect. Do not use NSTimer, simply store an NSDate on didDeactivate and check elapsed time in willActivate.

Easier said then done. I already built my whole app based on NSTimer. I'll wait for someone at Apple to address this. Lets see what they have to say

Filed my first bug report or RADAR as you savvy developers call it: #22962731

PrinceCreed's suggestion is easy to implement.

A few lines of code to save amd restore the timer's state and the rest of your logic is untouched.

I dont think it will work because my timers are stages. I also tried a combination of WKInterfaceTimer and NSTimer, but thats not a good solution.


So for example if I set an NSDate and restore it. I wont know how far along the routine I'm. My current implementation goes from stage to stage as the timer == 0 and then starts another timer.


I would love to sit with someone for a few minutes to discuss this.

"I would love to sit with someone for a few minutes to discuss this."


I've said that many times but these forums are about all the help available.


How do you calculate how far along you are?

I would assume that relies somewhat on the timer so restoring the timer should restore that variable.

It shouldn't be hard to keep track of which timer is currently running.

Ah you have no idea how long it took me to write a work around. I did end up using NSDate to check how much time has elapsed and run a loop through my stages to check how far along I'm. What a hassle.


This did solve my timer problem but wont fix the no haptic feedback when the watch is locked or app isnt active. Lots of users complaining that they arent getting any feedback.

Hey,


Was wondering if you have an AppleWatch and willing to test the timer implementation?


https://itunes.apple.com/app/id980247998

Prince,


Just to be cleear even with a workoutSession started an NSTimer will NOT continue when the watch locks (ie app is still active in "foreground"?) but not literally visable?