Saturday, August 11, 2012

Timer General

In the .h file:
        NSTimer *showInfoBoxAfterDelayTimer1;
In the .m file:
                myTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.5          //seconds (use decimal places if requried)
                                                                                                                                        target:self
                                                                                                                                  selector:@selector(someMethodName)
                                                                                                                                  userInfo:nil
                                                                                                                                   repeats:NO];         //NO = fire only once
                //[myTimer1 fire];  //< Use this to trigger it immediately

- (void)someMethodName
{

To Stop A Timer

    //You can use this, but if you do it to a timer that has already been invalidated you crash
        [myTimer1 invalidate];

    //This avoids that
    if ((myTimer1 != nil) && ([myTimer1 isValid]))
    {
        [myTimer1 invalidate];     //Causes release
        myTimer1 = NULL;
    }

Don’t Use This To Stop A Timer Or To Check It’s Been Released

Don’t use it even after a timers only fire event! This can cause non descript crashing bugs that can be horrible to realize are actually beind caused the release of the timer!! Use the method above not this:
        if (myTimer1)
        {
                [myTimer1 release];
                myTimer1 = nil;
        }

No comments:

Post a Comment