NSTimer Not Firing? [duplicate] - ios

This question already has answers here:
NSTimer not firing
(3 answers)
Closed 8 years ago.
My timer only fires once even though I have repeat initialized as YES. I want my timer to fire every 6 seconds? What is the problem?
#interface TCAMyScene (){
NSTimer *bombTimer;
}
bombTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:6 target:self selector:#selector(setBomb) userInfo:nil repeats:YES];
[bombTimer fire];
Solution:
bombTime = [NSTimer scheduledTimerWithTimeInterval:6 target:self selector:#selector(setBomb) userInfo:nil repeats:YES];

From the documentation for NSTimer:
You must add the new timer to a run loop, using addTimer:forMode:.
Upon firing, the timer sends the message aSelector to target. (If the
timer is configured to repeat, there is no need to subsequently re-add
the timer to the run loop.)
You're better calling:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
As this will add it to the current run loop in the default mode (which is usually what you want, unless there's an explicit need for a different run loop or mode)

Related

ns timer duplicate created 2-3 times

I am working on a app in which i have created a timer which called method make sound every 1 sec..
after 10 sec it is invalidated automatically but on one button click event i have stopped timer before 10 sec complete,but when again i come to that view it creates two timer and the call method twice and for third it called same method 3 times
DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
and i have invalidate that like this on button click event
[DecilneTimer invalidate];
DecilneTimer=nil;
how to solve duplicate creation of timer???
You should use:
self.DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
Using self.propertyName = ... you are using the property accessor and retaining the NSTimer automatically so that it doesnt get created again and again. And what you were doing was simply changing the ivar value directly

Tick timer in Objective C / iPhone? [duplicate]

This question already has answers here:
How do I use NSTimer?
(6 answers)
Closed 9 years ago.
I am making a Simon Says app to learn more about Objective C.
My SimonSaysViewController has 4 buttons. Their image needs to change accordingly when the pattern is being shown to the user.
A fixed interval timer will be absolutely fine.
I just cannot seem to find an example.
I basically would want a sort of setup like:
TimerTicked callback when I can do the image swapping logic.
Ideally, the TimerTicked method would be a method of my SimonSaysViewController.
How would this be done?
Thanks
NSTimer is your friend! Add an NSTimer property to your SimonSaysViewController.
#property (strong, nonatomic) NSTimer *tickTockTimer;
Depending on when you want the timer to start, you'll want to set up the timer then. Say you wanted the timer to start when the view first appears:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
}
Then implement the timerFired method and do what you need there.
- (void)timerFired:(NSTimer *)timer {
//change the image.
}
Don't forget to invalidate the timer when you are done.
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
This kind of thing usually works for me
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:#selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

Weird behavior of NSTimer in class

I have a real weird behavior of NSTimer object.
I'm trying to call ChangePic method with repeat counts of 2 seconds, But the timer isn't repeating. and i have this problem only in 1 class in the project
I have 5 View Controllers in my App, and the same code works in all classes except this one.
Does anybody has idea how could it be? is it possible that something blocking the timer ?
-BTW, ChangePic Method is called only once, and not repeating.
My Code:
ViewController.h
#property (nonatomic, strong) NSTimer *timer;
ViewController.m
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
[_timer fire];
-(void) ChangePic {
NSLog(#"testing");
}
Assign it to the property not the backing variable.
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
It's difficult to say exactly what your issue is given that there isn't a whole lot of background given. I can tell you that I answered another question where the problem turned out to be the fact that one of the views was blocking the timer. Timers can run on a number of different modes and different scenarios can cause the timer to sometimes be blocked. Try adding the timer to all common modes like this:
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
Also, like #Hejazi said, remove the 'fire' method call. It's not needed on a scheduled timer.

iOS NSTimer not working? [duplicate]

This question already has answers here:
NSTimer timerWithTimeInterval: not working
(4 answers)
Closed 8 years ago.
This is my exact code, and it doesn't seem to be working. Can you tell me what I am doing wrong? Note that refreshTimer was already declared in the private interface.
-(void)viewDidLoad {
refreshTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(timerTest) userInfo:nil repeats:YES];
}
-(void)timerTest {
NSLog(#"Timer Worked");
}
Give scheduledTimerWithTimeInterval a try:
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myMethod) userInfo:nil repeats:YES];
Quoting: NSTimer timerWithTimeInterval: not working
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.
There is two-option.
If using a timerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
also mode is two-option. NSDefaultRunLoopMode vs NSRunLoopCommonModes
more Information. refer a this documentation: RunLoopManagement
If using a scheduledTimerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
scheduled timers are automatically added to the run loop.
more information. refer a this documentation: Timer Programming Topics
In summary
The "timerWithTimeInterval" you have to remember
to add the timer to the run loop that you want to add on.
The "scheduledTimerWithTimeInterval" default auto creates a timer that runs in
the current loop.

How to stop NStimer event? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
In my application I am using NStimer to call an animation function every 3 seconds. I want to stop this timer and called another event while the timer is still running. Is this possible?
#interface
NSTimer *autoTimer;
#implementation
// Start timer
autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0)
target:self
selector:#selector(autoTimerFired:)
userInfo:nil
repeats:YES];
// Stop timer:
[autoTimer invalidate];
autoTimer = nil;
First, you want to keep a pointer to the timer
self.packetTimer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT target:self selector:#selector(connectionTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:packetTimer forMode:NSDefaultRunLoopMode];
If somewhere else in your code you want to cancel it, just call:
[self.packetTimer invalidate];
self.packetTimer = nil;

Resources