简体   繁体   English

如何停止我不知道的NSTimer是否已发布

[英]How to stop an NSTimer that I don't know is released or not

Sorry to ask this question, but it's now day 3 I try to solve this problem and have no progress so far. 很抱歉提出这个问题,但是现在是第三天,我试图解决这个问题,到目前为止还没有任何进展。

The problem is this: during a game there is a pause between the user answered a question and the next question. 问题是这样的:在游戏过程中,用户回答一个问题和下一个问题之间存在停顿。 Also in several other cases there are such pauses in gameplay. 同样在其他几种情况下,游戏中也会出现这种暂停。 For this I use one NSTimer. 为此,我使用一个NSTimer。

In .h I have: 在.h中,我有:

@property(nonatomic,retain) NSTimer *scheduleTimer;

and in the .m 并在.m中

@synthesize scheduleTimer;

scheduleTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target: self selector: @selector(playFeedbackSound) userInfo: nil repeats: NO];

now this works just fine. 现在这很好用。 But when the user exits the ViewController I need to invalidate the timer. 但是,当用户退出ViewController时,我需要使计时器无效。 Otherwise the timer will fire and then crash the app or pops up stuff that does not belong in the other view etc. 否则,计时器将触发,然后使应用程序崩溃或弹出不属于其他视图等的内容。

Therefore I write: 因此我写:

- (void)viewWillDisappear:(BOOL)animated {
    [scheduleTimer invalidate];   
}

now this does the job if the timer is actually set. 现在,如果确实设置了计时器,就可以完成此工作。 But if there is no such timer scheduled, the app just crashes. 但是,如果没有安排这样的计时器,则应用程序将崩溃。

I tried probably everything there is, including @try (which crashes the app too, Zombie says " * -[CFRunLoopTimer invalidate]: message sent to deallocated instance 0x567640"). 我尝试了可能存在的所有内容,包括@try(它也使应用程序崩溃,Zombie说“ * -[CFRunLoopTimer无效]:消息发送到已释放实例0x567640”)。 Since the timer gets released after it is done, a [scheduleTimer isValid] will just crash the app as well. 由于计时器是在完成后释放的,因此[scheduleTimer isValid]也将使应用程序崩溃。

Now I'm already pretty desperate, and as a last resort I'm thinking of replacing the timer with UIView animateWithDuration that does nothing visible. 现在我已经非常绝望了,作为最后的选择,我正在考虑将计时器替换为不可见的UIView animateWithDuration。

However, I think this should be a pretty standard situation. 但是,我认为这应该是一个非常标准的情况。 I just don't know why I can't find an answer to this very obvious task. 我只是不知道为什么我找不到这个非常明显的任务的答案。 Can you help? 你能帮我吗? Thank you 谢谢

I think the problem is that the NSTimer gets autoreleased before you invalidate it. 我认为问题在于,在使NSTimer invalidate之前,它会自动释放。

So you should do: 因此,您应该执行以下操作:

scheduleTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target: self selector: @selector(playFeedbackSound) userInfo: nil repeats: NO] retain];

And you should also release the timer in viewWillDisappear: 并且您还应该在viewWillDisappear: release计时器viewWillDisappear:

[scheduleTimer release];

But an even better solution is probably to use the dot property syntax to care of retain/release: 但是,更好的解决方案可能是使用dot属性语法来保留/释放:

self.scheduleTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target: self selector: @selector(playFeedbackSound) userInfo: nil repeats: NO];

And then: 接着:

- (void)viewWillDisappear:(BOOL)animated {
    if (self.scheduleTimer != nil) {
        [self.scheduleTimer invalidate];
        self.scheduleTimer = nil;
    }
}

Create a method to invalidate the timer that also sets the property to nil: 创建使计时器无效的方法,该方法还将属性设置为nil:

- (void) invalidateTimer
{
    if (self.scheduleTimer) {
       [self.scheduleTimer invalidate];   
       self.scheduleTimer = nil;  
    }
}

... and then call that method whenever you invalidate the timer. ...,然后在您使计时器无效时调用该方法。 For example: 例如:

- (void)viewWillDisappear:(BOOL)animated 
{
   [super viewWillDisappear: animated];
   [self invalidateTimer];
}

Make sure your timer is retained by using: 使用以下方法确保保留计时器:

self.scheduleTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target: self selector: @selector(playFeedbackSound) userInfo: nil repeats: NO];

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

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