ios - NSTimer's isValid always returns YES -


so i've got timer not repetitive. each time fires, method being executed decide if reschedule or not according inner logic of app.

this method available other parts of app, first thing i'm doing in method check if timer still valid (to know if initiator timer or different entity) in case wasn't initiated timer want invalidate it:

if (self.pollingtimer.isvalid) {     [self.pollingtimer invalidate];     self.pollingtimer = nil; } 

i've noticed if method being called due timer being fired - receive true value isvalid property, though when looking @ nstimer documentations under scheduledtimerwithtimeinterval:(nstimeinterval)seconds target:(id)target selector:(sel)aselector userinfo:(id)userinfo repeats:(bool)repeats method:

repeats
if yes, timer repeatedly reschedule until invalidated. if no, timer invalidated after fires.

discussion
after seconds seconds have elapsed, the timer fires, sending message aselector target.

i'm having hard time understand when timer being automatically invalidated bring me questions:

  1. any idea why yes isvalid?
  2. what exact definition of the timer fires? sending message aselector target stated in documentation? or finishing execution of method? (which might explain i'm experiencing)

thanks in advance.

a timer not real-time mechanism; fires when 1 of run loop modes timer has been added running , able check if timer’s firing time has passed. therefore, timer not invalidate itself, @ end of run loop.

as simple test, can see:

- (void)viewdidload {   [super viewdidload];   _timer = [nstimer scheduledtimerwithtimeinterval:0.5 target:self selector:@selector(timerfired) userinfo:nil repeats:no];    dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1 * nsec_per_sec)), dispatch_get_main_queue(), ^{     if (self.timer.isvalid){         nslog(@"--> timer valid");     } else {         nslog(@"timer invalid!");     }   }); }  - (void) timerfired {   if (self.timer.isvalid){      nslog(@"--> timer valid");   } else {      nslog(@"timer invalid!");   } } 

this log --> timer valid timerfired method , when block dispatch_after called, see timer invalid!. so, when schedule timer repeats:no, is guaranteed not reschedule not invalidate immediately.

so, answer question:

repeats

if yes, timer repeatedly reschedule until invalidated. if no, timer invalidated after fires (but not immediately)


Comments