0

how do i get the date from appEnterBackground and take away from appEnterForeground, then show the difference in a label. This is my code so far..

**.h**
    NSTimeInterval appEnteredBackground;
    NSTimeInterval appEnteredForeground;
    NSTimeInterval difference;  

**.m**

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    appEnteredBackground = [NSDate timeIntervalSinceReferenceDate];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    appEnteredForeground = [NSDate timeIntervalSinceReferenceDate];
    difference = appEnteredForeground - appEnteredBackground;

    NSLog(@"Duration is %@",[NSDate dateWithTimeIntervalSinceReferenceDate: difference]);
    NSLog(@"Duration is %@", [NSString stringWithFormat:@"%f", difference]);

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    NSString *time = [NSString stringWithFormat:@"%f", difference];   **//ERROR HERE (variable not used)**

    [dateFormatter release];
}

Any help would be fantastic

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
myles
  • 75
  • 1
  • 1
  • 8

2 Answers2

0

To measure this type of event (or difference of events), you need something with resolution of less than a second. There is a "c" interface to get the machine time, which is the hardware free running "ticks" inside the processor.

You can get really fine timing (milliseconds, microseconds if you believe them) using this StopWatch class.

StopWatch.h

#import <Foundation/Foundation.h>


@interface StopWatch : NSObject 
{
    uint64_t _start;
    uint64_t _stop;
    uint64_t _elapsed;
}

-(void) Start;
-(void) Stop;
-(void) StopWithContext:(NSString*) context;
-(double) seconds;
-(NSString*) description;
+(StopWatch*) stopWatch;
-(StopWatch*) init;
@end

StopWatch.m

#import "StopWatch.h"
#include <mach/mach_time.h>

@implementation StopWatch

-(void) Start
{
    _stop = 0;
    _elapsed = 0;
    _start = mach_absolute_time();
}
-(void) Stop
{
    _stop = mach_absolute_time();   
    if(_stop > _start)
    {
        _elapsed = _stop - _start;
    }
    else 
    {
        _elapsed = 0;
    }
    _start = mach_absolute_time();
}

-(void) StopWithContext:(NSString*) context
{
    _stop = mach_absolute_time();   
    if(_stop > _start)
    {
        _elapsed = _stop - _start;
    }
    else 
    {
        _elapsed = 0;
    }
    NSLog([NSString stringWithFormat:@"[%@] Stopped at %f",context,[self seconds]]);

    _start = mach_absolute_time();
}


-(double) seconds
{
    if(_elapsed > 0)
    {
        uint64_t elapsedTimeNano = 0;

        mach_timebase_info_data_t timeBaseInfo;
        mach_timebase_info(&timeBaseInfo);
        elapsedTimeNano = _elapsed * timeBaseInfo.numer / timeBaseInfo.denom;
        double elapsedSeconds = elapsedTimeNano * 1.0E-9;
        return elapsedSeconds;
    }
    return 0.0;
}
-(NSString*) description
{
    return [NSString stringWithFormat:@"%f secs.",[self seconds]];
}
+(StopWatch*) stopWatch
{
    StopWatch* obj = [[[StopWatch alloc] init] autorelease];
    return obj;
}
-(StopWatch*) init
{
    [super   init];
    return self;
}

@end

The class has a static stopWatch method that returns an autoreleased object.

Once you call start, use the seconds method to get the elapsed time. Call start again to restart it. Or stop to stop it. You can still read the time (call seconds) anytime after calling stop.

Example In A Function (Timing call of execution)

-(void)SomeFunc
{
   StopWatch* stopWatch = [StopWatch stopWatch];
   [stopWatch Start];

   ... do stuff

   [stopWatch StopWithContext:[NSString stringWithFormat:@"Created %d Records",[records count]]];
}
FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28
0

I think that you do not have errors here. You are correctly calculating the time difference, then you build a string with its "description" but do not use it.

To see if it all works correctly try this before the end of the method:

NSLog(@"Computed time was: %@", time);

You should care about that time variable, since you are not actively using it.

marzapower
  • 5,531
  • 7
  • 38
  • 76