NSTimer Not Stopping When Invalidated

后端 未结 7 1286
情书的邮戳
情书的邮戳 2021-01-05 05:22

I have the following code in my .h file:

#import 
#import 
#import 

        
相关标签:
7条回答
  • 2021-01-05 05:43

    Have you try to put repeat as No

    self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                                 target:self
                                                              selector:@selector(checkForMessages)
                                                               userInfo:nil
                                                                repeats:NO];
    
    0 讨论(0)
  • 2021-01-05 05:50

    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];
        }
    });
    
    0 讨论(0)
  • 2021-01-05 05:50

    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.

    0 讨论(0)
  • 2021-01-05 05:55

    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];
    
    0 讨论(0)
  • 2021-01-05 06:00

    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....)

    0 讨论(0)
  • 2021-01-05 06:04

    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. ;-)

    0 讨论(0)
提交回复
热议问题