Nstimer Not Stopping When Invalidated in This Function

NSTimer Not Stopping When Invalidated

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 does not stop on invalidate

don't need to schedule a timer each time, schedule it once and timer will fire every second for example u can do like below,

- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorOnMainThread:@selector(startTimerUpdate) withObject:nil waitUntilDone:NO]; //to start timer on main thread
}

//hear schedule the timer
- (void)startTimerUpdate
{
self.mytimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
}

- (void) updateProgressBar :(NSTimer *)timer{
static int count =0;
count++;
NSLog(@"count = %d",count);
if(count<=10)
{
//self.DownloadProgressBar.progress= (float)count/10.0f;
NSLog(@"progress:%f",(float)count/10.0f);
}
else
{
NSLog(@"invalidating timer");
[self.mytimer invalidate];
self.mytimer = nil;
return;
}
if(count <= 10){
NSLog(@"count = %d **",count);
}
}

NSTimer doesn't stop with invalidate

Read documentation for NSTimer:

There are three ways to create a timer:

  1. Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

  2. Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

  3. Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)

You are using method which already adds it to mainLoop from 1. - you need to remove this line or create a timer with 2. approach and leave manual adding.

Also remember that you must send invalidate message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

NSTimer doesn't stop

I should have been keeping a link to the timer by retaining it. :)
I've asked this question 5 months ago, and it's amazing, how much more experience I acquired. :)

timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
...
...
[timer invalidate];
[timer release];

Invalidating NSTimer not working

This is because you compare countDown with Int value. If you need invalidate timer when it's fired - do it inside subtractTime method.

You also mentioned that you setup your timer for 150 seconds. But in code example it is 1.0. So I suggest you want your selector be called 150 times with one second delay. If so, you could simply add counter variable:

var counter = 0
...
func subtractTime() {
counter += 1
if counter == 150 {
countDown.invalidate()
countDown = nil // also add this line to escape retain cycle
return
}
...
}

How to stop/invalidate NStimer

Declare NSTimer *myTimer in .h file.

Assign instance as tom said like this

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];

Stop and Invalidate using this

- (void) viewDidDisappear:(BOOL)animated
{
[myTimer invalidate];
myTimer = nil;
}

How to invalidate an NSTimer that was started multiple times

You can add timer.invalidate() before starting a new timer in startTimerButtonTapped if you want to reset the timer each time the "start" button is tapped:

@IBAction func startTimerButtonTapped(sender: UIButton) {
timer.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
}

I was going to update with an explanation but @jcaron already did it in the comment, so I'm just quoting his text, no need to change it:

Every time you tap on the "Start Timer" button, you create a new timer, while leaving the previous one running, but with no reference to it (since you've overwritten timer with the new timer you just created). You need to invalidate the previous one before you create the new one.

Invalidate NSTimer with block which has not been fired yet : Objective-C

You should make sure you invalidate the prior timer (if any) before instantiating a new timer, e.g.:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([resizeTimer isValid])
[resizeTimer invalidate];

resizeTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 block:^{
...
} repeats:NO];

....
}

Invalidating NSTimer

Try to create a separate NotificationTimer class and used its shared object all over the project like this way.

class NotificationTimer: NSObject {

var timer1: NSTimer?
var timer2: NSTimer?
var timer3: NSTimer?
var timer4: NSTimer?

static let sharedManager = NotificationTimer()

func getEvents(){
print("Fired")
if (timer1 == nil){
timer1 = NSTimer.scheduledTimerWithTimeInterval(20.0, target: self, selector: Selector("methodYouWantToCall"), userInfo: nil, repeats: true)
}
}
}

Now call this timer1 object inside any ViewController like this way.

let notificationTimer = NotificationTimer.sharedManager
notificationTimer.timer1?.invalidate()

Or call that method like this way

NotificationTimer.sharedManager().getEvents()


Related Topics



Leave a reply



Submit