简体   繁体   English

无法使计时器无效

[英]Unable to invalidate timer

I am trying to use a timer in my application where I want to start a timer on viewWillAppear and want to stop it in the viewWillDisappear method. 我试图在我的应用程序中使用计时器,在该应用程序中我想在viewWillAppear上启动计时器,并希望在viewWillDisappear方法中停止计时器。 I have done following coding: 我做了以下编码:
in .h: @property (nonatomic, strong) NSTimer *timer; 在.h中:@属性(非原子的,强的)NSTimer * timer;

-(void)viewWillAppear:(BOOL)animated{
   [super viewWillAppear:FALSE];
   if(!_timer){
     _timer = [NSTimer scheduledTimerWithTimeInterval: 5.0
                                              target: self
                                            selector:@selector(reloadInboxMessages)
                                            userInfo: nil repeats:YES];
  }
}

-(void)reloadInboxMessages {
  NSLog(@"reloadInboxMessages");
  [tblvwMessage reloadData];
 }

-(void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:FALSE];

   NSLog(@"viewWillDisappear");
   if([_timer isValid]){
       [_timer invalidate];
       _timer = nil;
   }
}

I thought it would be simple task to do but I am not sure why it is not working. 我认为这将是简单的任务要做,但我不知道为什么它不工作。 What is the issue? 有什么问题

尝试self.timer = [NSTimercheduledTimerWithTimeInterval ...

There are three ways to create a timer: 有三种创建计时器的方法:

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. 使用ScheduledTimerWithTimeInterval:invocation:repeats:或ScheduledTimerWithTimeInterval:target:selector:userInfo:repeats:类方法来创建计时器,并将其安排在默认模式下的当前运行循环中。

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. 使用timerWithTimeInterval:调用:重复:或timerWithTimeInterval:目标:选择器:USERINFO:重复:类方法没有调度它在运行循环来创建计时器对象。 (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.) (创建计时器后,必须通过调用相应的NSRunLoop对象的addTimer:forMode:方法将计时器手动添加到运行循环中。)

Allocate the timer and initialize it using the initWithFireDate:interval:target:selector:userInfo:repeats: method. 分配计时器并使用initWithFireDate:interval:target:selector:userInfo:repeats:方法对其进行初始化。 (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.) (创建计时器后,必须通过调用相应的NSRunLoop对象的addTimer:forMode:方法将计时器手动添加到运行循环中。)

EDIT: 编辑:

You are using method which already adds it to mainLoop from - create a timer with second approach and leave manual adding. 您正在使用的方法已将其从以下方法添加到mainLoop中-使用第二种方法创建计时器并保留手动添加。

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. 如果从另一个线程发送此消息,则与计时器关联的输入源可能不会从其运行循环中删除,这可能会阻止线程正常退出。

EDIT: 编辑:

to invalidate the timer that is passed in the callback, but that won't occur within viewWillDisappear method:. 无效即在回调传递的计时器,但不会viewWillDisappear方法中出现:。 therefore, it doesn't quite apply in this scenario: 因此,它并不完全适用于这样的场景:

  • (void)onTimer:(NSTimer *)pTimer { [pTimer invalidate]; (空隙)的OnTimer:(的NSTimer *)pTimer {[pTimer无效]; } }

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM