iphone - 时间(app hide)和时间(app出现)之间的差异

时间:2011-05-27 14:47:41

标签: iphone objective-c nsstring xcode4

如何从appEnterBackground获取日期并从appEnterForeground中删除,然后显示标签中的差异。 到目前为止,这是我的代码..

**.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];
}

任何帮助都会很棒

2 个答案:

答案 0 :(得分:0)

我认为你这里没有错误。您正在计算时差,然后使用其“描述”构建一个字符串,但不要使用它。

要查看一切是否正常,请在方法结束前尝试:

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

你应该关心那个time变量,因为你没有积极地使用它。

答案 1 :(得分:0)

要测量此类事件(或事件的差异),您需要分辨率小于一秒的内容。有一个“c”接口来获取机器时间,这是处理器内部的硬件自由运行“滴答”。

使用此StopWatch类,您可以获得真正精确计时(如果您相信,则为毫秒,微秒)。

<强> 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

该类有一个静态stopWatch方法,它返回一个自动释放的对象。

致电start后,请使用seconds方法获取已用时间。再次致电start以重新启动它。或stop阻止它。您仍然可以在致电seconds后随时阅读时间(致电stop)。

功能中的示例(执行的定时调用)

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

   ... do stuff

   [stopWatch StopWithContext:[NSString stringWithFormat:@"Created %d Records",[records count]]];
}