Iphone SDK 3.1.2 を使用。オブジェクトを使用してファイルに情報を保存していNSKeyedArchiver
ます。私は4つの鍵を持っています。で 4 つのキーすべてのファイルからデータを読み取りapplicationDidFinshLaunching
、 でファイル (4 つのキーすべて) に書き戻しますapplicationDidTerminate
。また、ユーザーが情報を保存すると、特定のキーのデータが書き込まれます。
最初にファイルがない場合は、アカウントリストに配列を割り当てます。これにより、アカウントを追加できます。
ファイルに保存されたデータが定期的に失われるという問題があります。アカウントリストなどの 1 つのキーが影響を受ける場合もあれば、お気に入りリストが影響を受ける場合もあります。4つすべてではなく、1つのキーだけを保存すると悪化するようです。organsierを使用してファイルを表示したところ、実際にデータが欠落しています。うまくいかないことも多いです。なぜそんなに矛盾しているのかわかりません。また、xcode でデバッガーを終了すると、ファイル データ全体ではなく、最近の変更のみが失われることが予想されます。NSKeyedArchiver の全体は、おそらく別の永続的なストレージ戦略を考えると非常に不安定に思えます。誰もがこのようなものに出くわします。
- (void)viewDidLoad
{
if( self.accountList == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
self.accountList = array;
[array release];
}
if( self.phoneSettings == nil)
{
PhoneSettings* mySettings = [[PhoneSettings alloc] initSettings];
self.phoneSettings = mySettings;
[mySettings release];
}
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
NSMutableArray *archiveArray = [unarchiver decodeObjectForKey:kDataKey];
if(archiveArray != nil)
self.accountList = [archiveArray retain];
else
LOG("No archived accounts" );
NSMutableArray *archiveCallList = [unarchiver decodeObjectForKey:kCallListKey];
if(archiveCallList !=nil)
appDelegate.callList = [archiveCallList retain];
NSMutableArray *archiveFavouritesList = [unarchiver decodeObjectForKey:kfavouritesKey];
if(archiveFavouritesList != nil)
appDelegate.favouritesList = [archiveFavouritesList retain];
PhoneSettings* archiveSettings = [unarchiver decodeObjectForKey:kPhoneSettings];
if (archiveSettings != nil)
self.phoneSettings = [archiveSettings retain];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
}
次に
- (void)applicationWillTerminate:(NSNotification *)notification {
LOG("Application terminating");
SessionTalkAppDelegate* appDelegate = (SessionTalkAppDelegate*)[[UIApplication sharedApplication] delegate];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data];
[archiver encodeObject:self.accountList forKey:kDataKey];
[archiver encodeObject:appDelegate.callList forKey:kCallListKey];
[archiver encodeObject:appDelegate.favouritesList forKey:kfavouritesKey];
[archiver encodeObject:self.phoneSettings forKey:kPhoneSettings];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[archiver release];
[data release];
}