0

I will keep on adding NSTimer into run loop. If i didn't get response with in the specified time interval my timer expired method will then i will invalidate my timer.If i got response with in the time interval i am invalidating my timer

expirationTimer = [NSTimer timerWithTimeInterval:20 target:self selector:@selector(timerExpired:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:expirationTimer forMode:NSRunLoopCommonModes];
-(void)timerExpired:(NSTimer*)expired //This will execute only when timer is expired
{
     [expired] invalidate];
     expired = nil;
}

[expirationTimer invalidate];
expiration = nil;

After invalidating my timer, still my call back is going to TimerExpired method. Any help is appreciated

  • 1
    Also calling `[expired invalidate]` is not required on a non-repeating timer and `expired = nil` has no effect. You should call `expirationTimer = nil;` in the timer method, however and add a check before adding a new one (as per the duplicate). – trojanfoe Feb 10 '16 at 20:48
  • You might have missed [this](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/#//apple_ref/occ/instm/NSTimer/invalidate): **`invalidate`: Special Considerations You must send this 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.** – Marcus Adams Feb 10 '16 at 21:07

0 Answers0