0

I am having so much trouble with NSTimers. I have used them before, but this timer just does not want to fire.

-(void) enqueueRecordingProcess
{
    NSLog(@"made it!");
    NSTimer *time2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];

}

-(void) warnForRecording
{
    NSLog(@"timer ticked!");
    if (trv > 0) {
        NSLog(@"Starting Recording in %i seconds.", trv);
    }
}

I don't see why this won't run. I even tried this:

- (void)enqueueRecordingProcess
{
    NSLog(@"made it!");
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(warnForRecording) userInfo:nil repeats:YES];
}

 - (void)warnForRecording
{
    NSLog(@"timer ticked!");
}

What is wrong?

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
Ben Reed
  • 824
  • 1
  • 8
  • 26

3 Answers3

2

Not sure if this fixes it but from the docs:

The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
kevboh
  • 5,207
  • 5
  • 38
  • 54
  • I tried this as well. It still is not running. The only way I can make it run is using [timer fire], but that only makes it run once. – Ben Reed Jun 03 '12 at 23:45
  • 4
    @rbvcode are you running this via a command line app or through a Cocoa app? NSTimer won't work in a Command line app because there is no runloop. – Richard J. Ross III Jun 03 '12 at 23:46
  • @RichardJ.RossIII ok... that explains it. Thank you so much. Nobody explained to me that their is no run loop in a Command line app. I will need to process my app in a completely different way. Sorry for not knowing this. This is the first time that I have ever developed a command line app that needed a timer. Once again, Thanks for your assistance. – Ben Reed Jun 03 '12 at 23:56
  • 1
    To run the run loop, you can just call [[NSRunLoop runloop] run] or something like that at the end of main. – Ken Aspeslagh Jun 04 '12 at 02:24
  • Re: using the run loop in a command line program: see http://stackoverflow.com/questions/2154600/command-line-cocoa-app-no-gui – Jon Gauthier May 22 '13 at 04:37
0

I am not sure about you specifics, but you need to service the runloop in order to get events, including timers.

Grady Player
  • 14,399
  • 2
  • 48
  • 76
0
// Yes.  Here is sample code (tested on OS X 10.8.4, command-line).
// Using ARC:
// $ cc -o timer timer.m -fobjc-arc -framework Foundation
// $ ./timer
//

#include <Foundation/Foundation.h>

@interface MyClass : NSObject
@property NSTimer *timer;
-(id)init;
-(void)onTick:(NSTimer *)aTimer;
@end

@implementation MyClass
-(id)init {
    id newInstance = [super init];
    if (newInstance) {
        NSLog(@"Creating timer...");
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
            target:self
            selector:@selector(onTick:)
            userInfo:nil
            repeats:YES];
    }
    return newInstance;
}

-(void)onTick:(NSTimer *)aTimer {
    NSLog(@"Tick");
}
@end

int main() {
    @autoreleasepool {
        MyClass *obj = [[MyClass alloc] init];
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}
rmcghee
  • 419
  • 4
  • 6