简体   繁体   English

NSUserDefaults未写入磁盘

[英]NSUserDefaults are not being written to disk

I set some values to NSUserDefaults and if I retrieve them without exiting the app, everything works fine. 我为NSUserDefaults设置了一些值,如果我在不退出应用程序的情况下检索它们,一切正常。 When I restart the app, the changes disappear in 95% of the time. 当我重新启动应用程序时,更改会在95%的时间内消失。 In 5% of the time everything is being saved ok. 在5%的时间里,一切都被保存好了。

I'm facing this problem for the first time. 我第一次面临这个问题。 In my other apps I don't usually save NSString in NSUserDefaults. 在我的其他应用程序中,我通常不会在NSUserDefaults中保存NSString。 May the fact of saving NSString cause problems? 可能保存NSString会导致问题吗?

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. 谢谢。

在执行必要的更改后添加以下代码解决了以下问题:

[ [NSUserDefaults standardUserDefaults] synchronize];

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

NSuserDefualts was not saving my values on game crash. NSuserDefualts没有在游戏崩溃时保存我的值。 by using, 通过使用,
[ [NSUserDefaults standardUserDefaults] synchronize]; the vales are getting saved. vales正在保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM