いくつかの値を NSUserDefaults に設定し、アプリを終了せずに値を取得すると、すべて正常に動作します。アプリを再起動すると、95% の確率で変更が消えます。5% の時間で、すべてが正常に保存されています。
私は初めてこの問題に直面しています。私の他のアプリでは、通常、NSString を NSUserDefaults に保存しません。NSString を保存すると問題が発生する可能性がありますか?
これが私のコードです:
-(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];
}
ありがとう。