0

I save a value in NSUserDefaults when I put my app in background mode, and then when the app becomes active again the value is different.

I save the value:

- (void)appDidEnterBackground:(NSNotification *)notification {
    //Tiempo inicial de inactividad

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
    NSTimeInterval timestamp = ([[NSDate date]  timeIntervalSince1970] * 1000);

    [dispositivo setFloat:timestamp forKey:@"StartBackground"];

    NSLog(@"Start background: %f", timestamp);

    [[NSUserDefaults standardUserDefaults] synchronize];
}

Log: Start background: 1418731653366.276123

I want to recover the value:

- (void)appDidBecomeActive:(NSNotification *)notification {

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
    NSTimeInterval startDate = [dispositivo floatForKey:@"StartBackground"];
    NSLog(@"Start date: %f", startDate);
}

Log: Start date: 1418731716608.000000

This is the only place I use this value. Thank you for advance.

amurcia
  • 801
  • 10
  • 26

2 Answers2

2

NSTimeInterval is in double press on it and you will get

typedef double NSTimeInterval;

so you should save value in double not in float

[dispositivo setDouble:timestamp forKey:@"StartBackground"];

and to get data

NSTimeInterval startDate = [dispositivo doubleForKey:@"StartBackground"];

its preferable to save NSDate object :)

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
0

There is nothing went wrong because you are using [NSDate date]...it will updating the current time...

madhu
  • 961
  • 9
  • 21
  • I use [NSDate date] to save the date before I enter in background not later. Later I'm reading the value in nsuserdefaluts – amurcia Dec 16 '14 at 12:23
  • http://stackoverflow.com/questions/2013850/whats-the-optimum-way-of-storing-an-nsdate-in-nsuserdefaults...go through this once – madhu Dec 16 '14 at 12:31