ios - Managing Timers Passed as Variables -
i trying perform thought simple task. have handful of repeating timers need started , stopped.
i created methods starting , stopping timers, , attempt pass timers methods parameters.
the problem timers never seem stopped. ideas why might not working properly? thank you!
top of file:
#import "viewcontroller.h" nstimer *launchtimer; nstimer *transactiontimer;
starting method:
-(void) startingmethod { nsstring *urlstring = @"http://google.com"; [[asynchrequestservice sharedinstance] performasynchronousurlrequest:urlstring completion:^(bool success, nsstring *responsebody, nsstring *responsestatus) { if (success) { [self stopresponsetimer:launchtimer]; } else { [self startresponsetimer:launchtimer method:@selector(startingmethod) interval:10]; } }]; }
method start timer:
-(void)startresponsetimer:(nstimer *) timer method:(sel) method interval:(int) interval { [timer invalidate]; timer = nil; timer = [nstimer scheduledtimerwithtimeinterval:interval target:self selector:method userinfo:nil repeats:yes]; }
method stop timer:
-(void)stopresponsetimer:(nstimer *) timer { nslog(@"stop timer"); [timer invalidate]; timer = nil; }
make startresponsetimer
, `stopresponsetimer' take pointer object pointer intead.
-(void)startresponsetimer:(nstimer **) timer method:(sel) method interval:(int) interval { [*timer invalidate]; *timer = nil; *timer = [nstimer scheduledtimerwithtimeinterval:interval target:self selector:method userinfo:nil repeats:yes]; } -(void)stopresponsetimer:(nstimer **) timer { nslog(@"stop timer"); [*timer invalidate]; *timer = nil; }
then invoke
[self startresponsetimer:&launchtimer]; [self stopresponsetimer:&launchtimer];
this should make sure retain right nstimer object. note:it idea check pointer pointer object null in public method
Comments
Post a Comment