NSTimer not firing

Discussion in 'Mac Programming' started by eddjc, Jul 27, 2012.

  1. macrumors newbie

    Joined:
    Apr 12, 2007
    #1
    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:
    Code:
    #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

    Code:
    
    #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:
    Code:
    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
     
  2. macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #2
    I'm not certain myself, but as for a better forum for devs
    Stack Overflow
    That site has been almost as valuable to me as Apple's Developer Library. Seriously.
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Apr 12, 2007
    #4
    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 :)
     
  5. macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    I am just wondering something and it could just be me. Your Method

    Code:
    - (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?
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     
  7. macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    I see what you are saying gnasher729, thanks for the clarification.
     

Share This Page