I am using nested NSTimer in an application. I have two issues here.
- (void)updateLeftTime:(NSTimer *)theTimer
- (void)updateLevel:(NSTimer *)theTimer
is also calling by timer.- (void)viewDidLoad {
[super viewDidLoad];
tmLevel=[NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(updateLevel:) userInfo:nil repeats:YES];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
}
- (void)updateLevel:(NSTimer *)theTimer {
static int count = 1;
count += 1;
lblLevel.text = [NSString stringWithFormat:@"%d", count];
tfLeftTime.text=[NSString stringWithFormat:@"%d",ANSWER_TIME];
tmLeftTime=[[NSTimer alloc] init];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
[self playMusic];
}
- (void)updateLeftTime:(NSTimer *)theTimer {
static int timeCounter=1;
timeCounter+=1;
tfLeftTime.text=[NSString stringWithFormat:@"%d", (ANSWER_TIME-timeCounter)];
}
[tmLevel invalidate]
to cancel schedule of a timer.tmLevel=nil
immediately after (to avoid using the variable after the timer has been unscheduled and released by the Runloop)[tmLevel invalidate]
also before assigning a new NSTimer to the tmLevel variable (or else the previous timer will continue to run in addition to the new one)Note also that in your code you have useless allocations that are moreover creating a leak:
tmLeftTime=[[NSTimer alloc] init];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
here you allocate an NSTimer instance, store this instance in tmLeftTime
... and then immediately forget about this created instance to replace it with another one, created using [NSTimer scheduledTimerWithTimeInterval:...]
!
Therefore, the NSTimer created using [[NSTimer alloc] init]
is lost, and is creating a leak (as it will never be released).
Your first line is totally useless, it's kinda like you were doing
int x = 5;
x = 12; // of course the value "5" is lost, replaced by the new value
add the following lines when u want to reset the timer
[tmLeftTime invalidate];
tmLeftTime = nil;
you can also use
if ([tmLeftTime isValid]){
// the timer is valid and running, how about invalidating it
[tmLeftTime invalidate];
tmLeftTime = nil;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With