Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 27, 2012, 06:10 AM   #1
eddjc
macrumors newbie
 
Join Date: Apr 2007
NSTimer not firing

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
eddjc is offline   0 Reply With Quote
Old Jul 27, 2012, 06:13 AM   #2
ConCat
Banned
 
Join Date: Jul 2012
Location: In an ethereal plane of existence.
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.
ConCat is offline   0 Reply With Quote
Old Jul 27, 2012, 07:12 AM   #3
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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.
gnasher729 is offline   0 Reply With Quote
Old Jul 27, 2012, 07:35 AM   #4
eddjc
Thread Starter
macrumors newbie
 
Join Date: Apr 2007
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
eddjc is offline   0 Reply With Quote
Old Jul 27, 2012, 07:45 PM   #5
larswik
macrumors 65816
 
Join Date: Sep 2006
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?
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Jul 28, 2012, 07:56 AM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by larswik View Post
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?
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.
gnasher729 is offline   0 Reply With Quote
Old Jul 28, 2012, 03:30 PM   #7
larswik
macrumors 65816
 
Join Date: Sep 2006
I see what you are saying gnasher729, thanks for the clarification.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NSTimer Alternatives adimanic iPhone/iPad Programming 1 Jan 4, 2014 12:04 AM
NStimer in Background erdinc27 iPhone/iPad Programming 6 May 11, 2013 07:21 AM
Keydown isn't firing. Dreamspinner Mac Programming 5 Jan 13, 2013 07:46 AM
NSTimer erroneously invalidated ramy1989 Mac Programming 4 Dec 21, 2012 03:22 PM
Former employer says Apple made a mistake firing Scott Forstsall Rogifan Apple, Industry and Internet Discussion 3 Nov 13, 2012 01:20 PM

Forum Jump

All times are GMT -5. The time now is 10:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC