Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Static NSDate to determine passing time

I'm trying to set up a static-date to determine how many seconds have passed between each time I call this method.

Instant crash!

-(void) myMethod
{   
    static NSDate *staticDate = nil;
    NSTimeInterval seconds   = 0.0;

    if(staticDate)
    {   
        NSLog(@"%@", staticDate);
        seconds = [staticDate timeIntervalSinceNow];
    }

    staticDate = [NSDate date];
    NSLog(@"%.2f", seconds);
}
like image 596
Patricia Avatar asked Jan 31 '11 16:01

Patricia


1 Answers

Perhaps you'd be better of using timeIntervalSince1970, as its a commonly used method across many languages. It will return the number of seconds that have elapsed since the 1st January 1970.

Set up an instance variable to hold the first timeInterval and initialise it to the value returned by [[NSDate date] timeIntervalSince1970], then you could use it in your method like this:

-(void) myMethod
{
    NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970] - _initialTimeInterval;
    NSLog(@"Seconds = %.2f", seconds);
}

A posible reason why your current code may crash is because [NSDate date] returns an autoreleased object. Even though the variable is static, the autorelease pool is likely releasing the date object and then causing a crash when you try to access it the next time the method is run.

You may be able to skirt around this crash if you create a new date or retain the one returned from date. However, assigning a retained/owned object to a static variable will result in a memory leak and I suggest you try my solution instead.

like image 159
Jasarien Avatar answered Nov 14 '22 16:11

Jasarien