32

I've created a test application with timer before implementing it in my project. It was the first time I'm using timer. But the issue is when I implemented timer using [NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ]; , it is not working. Here is my code, Interface:

@interface uialertViewController : UIViewController
{

    NSTimer *timer;
}

-(void)displayAlert;
-(void)hideandview;
@end

Implementation:

@implementation uialertViewController
- (void)viewDidLoad {

    [self displayAlert];
    [super viewDidLoad];
}


-(void)displayAlert{

    timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(hideandview) userInfo:nil repeats:NO];

    alert = [[UIAlertView alloc] initWithTitle:@"testing" message:@"hi hi hi" delegate:nil cancelButtonTitle:@"continue" otherButtonTitles:nil];
    [alert show];




    [alert release];
    alert = nil;

}

-(void)hideandview{

    NSLog(@"triggered");

    [alert dismissWithClickedButtonIndex:0 animated:YES];

    [alert release];

    [self displayAlert];
}

@end

Then I Changed [NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ]; with [NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: ]; , It is working. What was the issue with timerWithTimeInterval: ? Am I mising anything in my first implementation ? Thanks in advance.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200

4 Answers4

129

scheduledTimerWithTimeInterval:invocation:repeats: and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: create timers that get automatically added to an NSRunLoop, meaning that you don't have to add them yourself. Having them added to an NSRunLoop is what causes them to fire.

With timerWithTimeInterval:invocation:repeats: and timerWithTimeInterval:target:selector:userInfo:repeats:, you have to add the timer to a run loop manually, with code like this:

[[NSRunLoop mainRunLoop] addTimer:repeatingTimer forMode:NSDefaultRunLoopMode];

Other answers on here suggest that you need to call fire yourself. You don't - it will be called as soon as the timer has been put on a run loop.

Joseph Humfrey
  • 2,974
  • 2
  • 23
  • 34
  • 2
    Even with the scheduled method, if running in a command line util, you still need to add it do the mainRunLoop by hand, at least in my experience, as well as indicated by : [run run run nsrunloop](http://hackazach.net/code/2013/08/09/run-run-run-nsrunloop/) – jheld Mar 31 '14 at 14:19
  • scheduledTimer worked perfectly with this suggestion without having to fire the timer. thank you – hitme Mar 04 '15 at 07:21
  • I tried to use the scheduledTimer method without firing it, but it did not work. But I totally agree with your answer that you don't have to add it to the run loop. Thank you for clarifying the difference. – Septronic Oct 31 '15 at 23:41
3

Also one may want to make sure to add timer on the main thread.

assert(Thread.isMainThread)
let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(YourSelector), userInfo: nil, repeats: true)
Vladimir Shutyuk
  • 2,956
  • 1
  • 24
  • 26
3

As a previous answer noted schedule on the main thread, but rather than using assert, put it on the main thread:

@objc func update() {
    ...
}

DispatchQueue.main.async {
            self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
        }

And if async is not desired, try this:

let schedule = {
            self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
        }

        if Thread.isMainThread {
            schedule()
        }
        else {
            DispatchQueue.main.sync {
                schedule()
            }
        }
Joe Pagliaro
  • 336
  • 3
  • 8
1

The difference between the two is that the timerWithTimeInterval method returns a NSTimer object that has not yet been fired. To fire the timer you have to use [timer fire]; On the other hand the scheduledTimerWithTimeInterval returns an NSTimer that has already been fired.

So, in your first implementation you were just missing [timer fire];

etolstoy
  • 1,798
  • 21
  • 33
Chance Hudson
  • 2,849
  • 1
  • 19
  • 22
  • 4
    See my post - `[timer fire]` does not "start" the timer, it simply directly calls that method. You need to add the timer to a run loop for it to start. – Joseph Humfrey Jul 01 '12 at 10:32
  • It works for once but for continuous process use @Joseph Humfrey code. – yazh May 06 '15 at 13:49