Tutorial :Storing NSTimer objects in NSMutableDictionary


using iphone sdk 4.o. I am attempting to store around 10 NSTimer object in a NSMutableDictionay and then index them by key. This is to save 10 diffent startTimer, stopTimer functions. I have done like this but am worried about memory leak issues

Is the below code safe, is it ok to copy timer objects into a dict. TimerList is a property of type NSMutableDictionary.

-(IBAction)startTimer:(NSNumber)identifier  {              NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:7                         target:self selector:@selector(timerFireMethod:)                         userInfo:nil  repeats:YES];          [self.TimerList setObject:timer forKey: identifier];   }      -(IBAction)stopTimer:(NSNumber)identifier  {              NSTimer* timer = [self.ReRegisterTimerList objectForKey: identifier];      [timer invalidate];      [self.TimerList setObject:nil forKey: identifier];    }    -(void)timerFireMethod:(NSTimer*)theTimer  {          if (theTimer == [self.TimerList objectForKey:someKey])      {          found = true;          // do something if its a certain timer                        }  }  


Note that you take class-type instances by pointer, i.e. it should be NSNumber* not NSNumber.

Otherwise it looks fine to me - just remember the special consideration for -invalidate:

You must send this message from the thread on which the timer was installed.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »