Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch OS2 NSTimer problems

I'm working on an App where I need to start a timer (using NSTimer) when the Watch is activated. With the Timer I asks some information to the iPhone (about every 1 seconds and maximum for 5 seconds). I'm using this to start the timer

timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(myfunction) userInfo:nil repeats:NO];

In the "myfunction" function, I restart the timer for the next time.

- (void) myfunction
{
   //Here I update a label text
   // [...]

   [timer invalidate];
   timer = nil;

   counter++;
   if(counter<5)
   {
     timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(myfunction) userInfo:nil repeats:NO];
   }
}

My problem is that in the simulator all works fine but in a real watch (Watch-OS2 GM) the timer sometimes doesn't start or sometimes it starts but only for one time and after seems freeze! I see this because i Update a label in the watch at every elapsed period that shows a counter and I'm sure all is initialized in the "will activate" function. I don't understand why. Someone with the same issue?

like image 252
Lorenzo Avatar asked Mar 20 '26 12:03

Lorenzo


1 Answers

From documentation

Use your interface controller’s init and awakeWithContext: methods to load any required data, set the values for any interface objects, and prepare your interface to be displayed. Do not use the willActivate to initialize your interface controller. The willActivate method is called shortly before your interface is displayed onscreen, so you should use that method only to make last-minute changes. For example, you might also use that method to start animations or start other tasks that should only happen while your interface is onscreen.

So, what method did you use to instantiate timer?

make sure you use willActivate method and also use the didDeactivate method to clean up your interface and put it into a quiescent state. For example, use this method to invalidate timers and stop animations.

Hope this helps

like image 126
Doro Avatar answered Mar 22 '26 01:03

Doro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!