views:

450

answers:

3

Hello,

I set some values to NSUserDefaults and if I retrieve them without exiting the app, everything works fine. When I restart the app, the changes disappear in 95% of the time. In 5% of the time everything is being saved ok.

I'm facing this problem for the first time. In my other apps I don't usually save NSString in NSUserDefaults. May the fact of saving NSString cause problems?

Here's my code:

    -(void) mayShowFeedbackMessage
    {
     NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
     [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
     [dateFormatter setDateStyle: NSDateFormatterMediumStyle];

     if ([ [NSUserDefaults standardUserDefaults] objectForKey: @"ratingsMessage_shown_on_date"])
     {
          NSString *lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                           objectForKey: @"ratingsMessage_shown_on_date"];
          NSDate *lastRatingDate = [dateFormatter dateFromString: lastRatingDateString];

          DebugLog(@"lastRatingDateString: %@", lastRatingDateString);
          DebugLog(@"time since last message: %.0f", [ [NSDate date] timeIntervalSinceDate: lastRatingDate]);

          if ( [ [NSDate date] timeIntervalSinceDate: lastRatingDate] >= 120.0f )
          {
              [self showFeedbackMessage];

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

              NSString *dateString = [dateFormatter stringFromDate: [NSDate date] ];
             [dateFormatter release];

             [ [NSUserDefaults standardUserDefaults] setObject: dateString
                                                        forKey: @"ratingsMessage_shown_on_date" ];

             lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                     objectForKey: @"ratingsMessage_shown_on_date"];
             DebugLog(@"new date: %@", lastRatingDateString);

         }
     }
     else
     {  
         NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
         [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
         [dateFormatter setDateStyle: NSDateFormatterMediumStyle];

         NSString *dateString = [dateFormatter stringFromDate: [NSDate date] ];
         [dateFormatter release];

         [ [NSUserDefaults standardUserDefaults] setObject: dateString
                                                        forKey: @"ratingsMessage_shown_on_date" ];

         NSString *lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                                objectForKey: @"ratingsMessage_shown_on_date"];
         DebugLog(@"new date: %@", lastRatingDateString);

     }

         [dateFormatter release];
}

Thanks.

+7  A: 

Adding the following code after doing the necessary changes solved the issue:

[ [NSUserDefaults standardUserDefaults] synchronize];
Ilya
+1  A: 

For what it's worth you can look at your defaults with the Property Editor to see if it's being written correctly:

open ~/Library/Preferences/org.your.app.plist

or just dump them:

defaults read org.your.app
nall
A: 

NSuserDefualts was not saving my values on game crash. by using [ [NSUserDefaults standardUserDefaults] synchronize]; the vales are getting saved.

g.revolution