NSTimer Not Stopping When Invalidated - objective-c

I have the following code in my .h file:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <MapKit/MapKit.h>
#interface LandingController : UIViewController<CLLocationManagerDelegate> {
CLLocationManager *LocationManager;
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) NSTimer *messageTimer;
- (IBAction)LoginButton:(id)sender;
#end
I have the following code in my .m file:
#interface LandingController ()
#end
#implementation LandingController
#synthesize messageTimer;
- (void)checkForMessages
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"BINGO:"
message:#"Bingo This Works"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)LoginButton:(id)sender {
if ([UserType isEqualToString:#"Owner"]) {
if (messageTimer){
[self.messageTimer invalidate];
self.messageTimer = nil;
}
} else {
if (!messageTimer){
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
}
}
}
#end
But my timer doesn't want to stop when I call the invalidate.
The LoginButton is only pressed twice, once when the strResult is = to "Guard" and then the application changes it to be equal to "Owner" and the user presses the login button again, so I don't think I'm setting multiple timers.
After pressing the login button and starting the timer I segue to another view and then segue back to press the login button once more which is when I want the timer to stop. Do I need to do anything special to get the messageTimer since I switched views for a moment and then came back?
Any ideas?
Thanks!

You need to call [self.messageTimer invalidate] on the same thread on which you created the timer. Just make sure that the timer is created and invalidated on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if ([UserType isEqualToString:#"Owner"]) {
[self.messageTimer invalidate];
self.messageTimer = nil;
} else {
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
}
});

NSTimer is retained by NSRunLoop, so the only way I see your issue happening is if you're actually creating more than one timer and invalidating only what you have reference to.
Example:
if(!timer)
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:#selector(processTimer) userInfo:nil repeats:YES];

Have you try to put repeat as No
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:NO];

If the code at the end (starting with if) is called twice with UserType != Owner, you create a new timer without invalidating the old one. Now two timers are running. If the code executes a third time, you will add a third timer and so on. Executing the code with UserType == Owner only invalidates the last timer and even it is called repeatly, it does not invalidate older timers.
You have a timer leak. ;-)

How about put an NSLog in your checkForMessages method? It would be easier to check if there's really only 1 timer.
(I'd rather put this in a comment, but I don't have that much reputation....)

I have an approach for stopping or deactivate the timer, Before apply this make sure that you tried all the cases as mentioned above so you can also understand that why this approach used at last.
// Instead of self use NSTimer class, it will not provide you any crash and in selector placed any empty function and setRepeats to NO
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:100.0
target:NSTimer selector:#selector(emptyFunctionCalling)
userInfo:nil
repeats:NO];
[self.messageTimer invalidate];
self.messageTimer = nil;
So whenever the case occured that timer will not stopping then entering in this code the timer will stops permanently.

Its weird but invalidating passed timer reference and created timer reference worked for me.
delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateDelayLable:) userInfo:nil repeats:YES];
-(void)updateSendDataDelayLable:(NSTimer*)timer{
delayValueForGNSS--;
if (delayValueForGNSSSend==0) {
[timer invalidate];
timer = nil;
[delayTimer invalidate];
delayTimer = nil;
}
}

Related

How do I set a NSTimer to stop if a certain action is running?

I have a popup in a custom view controller that is presented after 1 minute thanks to my NSTimer.
My NSTimer code:
[NSTimer scheduledTimerWithTimeInterval:60.0f
target:self selector:#selector(methodB:) userInfo:nil repeats:YES];`
The method that reveals the popup
- (void) methodB:(NSTimer *)timer
{
//Do calculations.
[self showPopupWithStyle:CNPPopupStyleFullscreen];
}
I'm trying to set the NSTimer to stop running if the [self showPopupWithStyle:CNPPopupStyleFullscreen]; is currently open, running or active.
Then I would like to start the NSTimer back up again if the popup view controller is NOT open, running or active.
Any help, samples or examples would be greatly appreciated!
My project is written in Objective-C.
EDIT:
I tried the answers in the suggested "possibly similar" answer and it doesn't seem to work for what I am doing. How do I stop NSTimer?
This seems to do the trick.
#property (nonatomic, strong) CNPPopupController *popupController;
- (void)_timerFired:(NSTimer *)timer;
#end
NSTimer *_timer;
- (void)viewDidLoad {
[super viewDidLoad];
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:#selector(_timerFired:)
userInfo:nil
repeats:YES];
}
}
- (void)_timerFired:(NSTimer *)timer {
if ([_timer isValid]) {
[self showPopupWithStyle:CNPPopupStyleFullscreen];
[_timer invalidate];
}
_timer = nil;
NSLog(#"ping");
}
If you want to restart the timer just add this code where you'd like the action to start back up.
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:#selector(_timerFired:)
userInfo:nil
repeats:YES];
}
Hope this helps! :)

Pause and resume Timer in ViewController

I want to pause and resume timer in viewcontroller which is displayed as subview in mainviewcontroller. Play/Pause button in mainviewcontroller when pressed it displays view controller as subview as well as plays audio.
Have this NSTimer in view controller
[NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
- (void)updateView:(NSTimer *)theTimer
{
if (index < [textArray count])
{
self.textView.text = [self.textArray objectAtIndex:index];
self.imageView.image = [self.imagesArray objectAtIndex:index];
index++;
} else {
index = 0;
}
I want to pause and resume timer when play/pause button is pressed in mainviewcontroller. How can i do that.
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
[myTimer invalidate];
/*
Stops the receiver from ever firing again and requests its removal from its run loop.
*/
[myTimer fire];
/*
You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
*/
First you have to keep a reference of your timer in your interface-file:
NSTimer *myTimer;
In the implementation file you can simply connect and then refer to it:
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
To stop the timer call:
[myTimer invalidate];
To start it again:
[myTimer fire];
See NSTimer Class Reference for more info about the NSTimer Class.

NSTimer stopwatch with specific control

I have a label 00:00.0 and button. When I press button the timer should stars counting on label it's no matter how many clicks I'll do. The timer should stop when I tap on label, and should reset on second tap on same label.
I tried everything, but I can't reach result :) Anybody please help me.
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
IBOutlet UILabel *stopWatchLabel;
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
}
#property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (void)updateTimer;
-(IBAction)onStartpressed:(id)sender{
NSTimer * timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1.0 target:self selector:#selector(methodthatUpdateslabel) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(IBAction)onStopPressed:(id)sender{
[timer invalidate];
}
This should do it hope it helps.
Quoting some code inside my active project.
-(void)start{
[self setup];
if(_timer == nil){
_timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireInterval target:self selector:#selector(updateLabel:) userInfo:nil repeats:YES];
_counting = YES;
}
}
-(void)pause{
[_timer invalidate];
_timer = nil;
_counting = NO;
}
That's the basic concept of implement a stopwatch, if you want to use UILabel as a stopwatch, MZTimerLabel is a perfect two line solution for you. Take a look.
Cheers.

NSTimer within a class

I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press - counts from 60 to 0 seconds.
I have the following code in a Timer.m class.
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently - the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return - but I can't seem to wrap my head around how to update the label in the ViewController with the returned data.
The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time... I can't get it to automatically count down...
Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:
If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller's action in your advanceTimer method?
Yet easier, you could connect your timer class to the label.
You might do it like this:
// Timer.h:
#interface Timer : NSObject
#property (retain, nonatomic) IBOutlet UILabel *timeLabel;
#property (assign, nonatomic) NSInteger secondsRemaining;
#property (assign, nonatomic) NSTimer *timer;
- (IBAction)startCountdown:(id)sender;
- (IBAction)stopCountdown:(id)sender;
- (void)timerFired:(NSTimer *)timer;
#end
// Timer.m
#implementation Timer
#synthesize timeLabel = timeLabel_;
#synthesize secondsRemaining = secondsRemaining;
#synthesize timer = timer_;
- (void)setTimer:(NSTimer *)timer
{
if (timer = timer_)
return;
[timer_ invalidate];
timer_ = timer;
}
- (void)scheduleTimer
{
if (self.secondsRemaining <= 0) {
self.timer = nil;
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO];
}
}
- (void)timerFired:(NSTimer *)timer
{
self.secondsRemaining -= 1;
NSString *displayString = [NSString stringWithFormat:#"%d", self.secondsRemaining];
self.timeLabel.text = displayString;
[self scheduleTimer];
}
- (IBAction)startCountdown:(id)sender
{
self.secondsRemaining = 60;
[self scheduleTimer];
}
- (IBAction)stopCountdown:(id)sender
{
self.timer = nil;
}
- (void)dealloc
{
[timeLabel_ release];
[super dealloc];
}
#end
This code has a two benefits:
You can cancel your timer.
Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.
Correct me if I'm wrong, but I think you should retain that NSTimer in a class member. Otherwise the timer is destroyed when finishing the method.
#property (nonatomic,retain) NSTimer * yourTimer;
In the .m file
#synthesize yourTimer;
And then
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
I fixed also your NSNumber alloc, so there are no memory leaks ;)
Explaining it, NSTimer scheduledTimerWithTimeInterval: gives you an NSTimer with autorelease. If this Timer is not retained by some member, it gets released as soon as the method ends and, since no other pointer is retaining it, it gets freed. Maybe that's the explanation ;). Never worked with NSTimers
The countdown class needs to save a link back to the ViewController and then call a method on it.
One approach would be to use the delegate pattern. Have the Countdown class's init method as initWithDelegate:(id)delegate and a predefined callback method (like updateCountdown:(NSNumber*)currentCountdown). The ViewController sends itself as the delegate and implements the update method.
Another approach is the target/action pattern. NSTimer uses this approach. The init method would be initWithTarget:(id)target selector:(SEL)selector. The ViewController sends itself as the target and whatever selector it wants to use (as long as it takes an NSNumber as it's sole argument).
In both cases in advanceTimer the Countdown class will use performSelector:withObject: to call the ViewController's update method.
If you really want a true one second timer then set it to repeat. Otherwise you will drift slowly by the amount of time the advanceTimer method takes to fire and complete. At the end of the countdown use a reference to the timer to invalidate it.

NSTimer doesn't call method

I'm really frustrated now, googled the whole internet, stumbled through SO and still didn't find a solution.
I'm trying to implement an NSTimer, but the method which I defined doesn't get called. (seconds are set correctly, checked it with breakpoints). Here is the code:
- (void) setTimerForAlarm:(Alarm *)alarm {
NSTimeInterval seconds = [[alarm alarmDate] timeIntervalSinceNow];
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
}
- (void) showAlarm:(Alarm *)alarm {
NSLog(#"Alarm: %#", [alarm alarmText]);
}
The object "theTimer" is deined with #property:
#interface FooAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {
#private
NSTimer *theTimer;
}
#property (nonatomic, retain) NSTimer *theTimer;
- (void) setTimerForAlarm:(Alarm *)alarm;
- (void) showAlarm:(Alarm *)alarm;
What am I doing wrong?
timerWithTimeInterval simply creates a timer, but doesn't add it to any run loops for execution. Try
self.theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
instead.
Also don't forget to check if
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
is called in the main thread.
You've created an NSTimer object but you haven't scheduled it to be run. timerWithTimeInterval:target:selector:userInfo:repeats: creates a timer that you can schedule to run later, for example, to create a timer at application launch and have it start counting when the user presses a button. Either call
[[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]
at the end of setTimerForAlarm or replace
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
with
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
which creates a timer and immediately schedules it.
Well you may want to actually schedule your NSTimer on the run loop :) instead of timerWithTimeInterval use scheduledTimerWithTimeInterval.
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
While all of the answers are right, there is an even simpler solution that doesn't involve a NSTimer at all. Your setTimerForAlarm: implementation can be reduced to one simple line:
[self performSelector:#selector(showAlarm:) withObject:alarm afterDelay:[[alarm alarmDate] timeIntervalSinceNow]]