(iphone) I need to invalidate the retry timer: no?

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];

If for retries: set to NO, do I need to invalidate the timer inside the specified selector?

thank

Edit

Another question, if it is itself invalid,

How to cancel such a timer?
Since the invalidation of an already invalid timer could be a failure, I guess?

keep a pointer to a timer and set it to zero inside the selector to be started?

+3
source share
5 answers

No, the timer itself will invalidate

+6
source

@Eugene if you use

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:YES];

then in the selector method you need to provide a function like this

- (void)timerFireMethod:(NSTimer*)theTimer

, , ,

if(workDone == YES)
{
   [theTimer invalidate];
}

NO , .

+1

You can save the flag to save whether the timer was started or not.

eg.

    BOOL gameOver = NO;
    NSTimer * gameOverTimer;


-(void)startGame
{

     gameOverTimer =    [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(stopLevel:) userInfo:nil repeats:NO]; 
     // your code
}

-(void)stopLevel:(id)sender
{
     gameOver = YES;
     // your code
}

-(void)levelFinishedSuccesfully
{
     // this method will get called if user finishes the level before your timer ends/stops the level. So the timer is valid and we need to invalidate it
     if(!gameOver)
     {
          [gameOverTimer invalidate];
          gameOverTimer = nil;
     }    
      // your code
}

Hope this helps.

+1
source

If YES repeats, the timer will re-redirect itself until canceled. If NO, the timer will be invalid after it occurs.

0
source

you are not enough to add a timer source to your runloop

addTimer: forMode:

0
source

Source: https://habr.com/ru/post/1796236/


All Articles