PDA

View Full Version : NSTimer not firing




eddjc
Jul 27, 2012, 06:10 AM
Hello all,

Hope this is the right place to ask (if there is a better development forum please let me know!)

I've got a basic "tick" program that uses an NSTimer, currently running it in a "foundation" app (on the terminal). For some reason NSTimer doesn't seem to be firing:

#import <Foundation/Foundation.h>

@interface ECMetronome : NSObject
{
NSTimeInterval millisecondsBetweenTicks;
NSTimer *timer;
NSNotificationCenter *notificationCenter;
NSInvocation *invocation;


}

@property (nonatomic, retain) NSTimer * timer;
- (id) initWithMilliseconds: (double) milliseconds;
- (id) initWithMetronomeMark: (double) mark;

- (void) start;
- (void) tick: (NSTimer *)theTimer;

@end




#import "ECMetronome.h"

@implementation ECMetronome
@synthesize timer;

- (id)init
{
self = [super init];
if (self) {

if (!millisecondsBetweenTicks) millisecondsBetweenTicks = 1000;


NSLog(@"Metronome Initialised - metronome mark:%f", millisecondsBetweenTicks/1000*60);

}

return self;
}

- (id) initWithMilliseconds: (double) milliseconds
{
millisecondsBetweenTicks = milliseconds;
return [self init];
}

- (void) setMetronomeMark:(double)mark
{
millisecondsBetweenTicks = 60000/mark;
}

- (id) initWithMetronomeMark: (double) mark
{
millisecondsBetweenTicks = 60000/mark;
return [self init];
}


- (void) start
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:(millisecondsBetweenTicks/1000) target:self selector:@selector(tick:) userInfo:nil repeats:YES];
NSLog(@"Timer Started");
}


- (void) tick: (NSTimer *)theTimer {

NSLog(@"Tick!");
}

@end



Output is:
2012-07-27 01:56:07.573 commandLineMetronome[7396:303] Hello, World!
2012-07-27 01:56:07.575 commandLineMetronome[7396:303] Metronome Initialised - metronome mark:60.000000
2012-07-27 01:56:07.575 commandLineMetronome[7396:303] Timer Started

and then it stops running. Any ideas?

Edd



ConCat
Jul 27, 2012, 06:13 AM
I'm not certain myself, but as for a better forum for devs
Stack Overflow (http://stackoverflow.com/)
That site has been almost as valuable to me as Apple's Developer Library. Seriously.

gnasher729
Jul 27, 2012, 07:12 AM
Do you have a runloop at all?
Any event processing?

Did your program just exit?

Without a runloop, timers won't do anything. And when the program exits, timers will obviously not be called anymore. Timers don't keep your program alive.

eddjc
Jul 27, 2012, 07:35 AM
Ahh I did see something about that in the Class Reference but didn't quite understand how it related/what I should do - Do I need to attach the timer to the run-loop in some way? Presumably if they're running on the same thread then they need to be at least aware of each other... (yes the program just exited)

Thanks
Edd

----------

Concat - thanks for the tip! Will use it.

Edd :)

larswik
Jul 27, 2012, 07:45 PM
I am just wondering something and it could just be me. Your Method


- (void) tick: (NSTimer *)theTimer {

NSLog(@"Tick!");
}


excepts an argument theTimer of type NSTimer. But the timer code that executes your repeat timer sends no argument to that Method that I could see? If I were writing that method I would just have -(void) tick{}. Is your intention to pass some parameter at some point to this method?

gnasher729
Jul 28, 2012, 07:56 AM
I am just wondering something and it could just be me. Your Method


- (void) tick: (NSTimer *)theTimer {

NSLog(@"Tick!");
}


excepts an argument theTimer of type NSTimer. But the timer code that executes your repeat timer sends no argument to that Method that I could see? If I were writing that method I would just have -(void) tick{}. Is your intention to pass some parameter at some point to this method?

No, the timer code in itself is right. scheduledTimerWithTimeInterval: repeat:YES will create a timer that sends the message to the target at the given interval, until the app quits or the timer is invalidated. HOWEVER this only works if you have a run loop running.

I suspect that the OP hasn't started with a "Cocoa application" project where you automatically have a runloop running. Without a run loop, you can do things like running tools, which do some task and then exit, but you can't have any useful user interface. And timers don't work.

larswik
Jul 28, 2012, 03:30 PM
I see what you are saying gnasher729, thanks for the clarification.