NSTimer Not Stopping When Invalidated

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

I have the following code in my .h file:

#import 
#import 
#import 

        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    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];
        }
    });
    

提交回复
热议问题