NSKeyedArchiver文件 - 丢失数据

时间:2010-01-28 16:28:08

标签: iphone nskeyedarchiver

使用Iphone SDK 3.1.2。我使用NSKeyedArchiver对象将一些信息保存到文件中。我有4把钥匙。 我从文件中读取applicationDidFinshLaunching上所有4个密钥的数据,然后回写applicationDidTerminate上的文件(所有4个密钥)。此外,当用户保存任何信息时,我会写入其特定键的数据。

如果最初没有文件我为帐户列表分配了一个数组,这允许我向其添加帐户。

我遇到文件问题,定期丢失保存到它的数据。有时它的一个键受影响即帐户列表,有时它是收藏夹列表。如果我只保存1键而不是所有4.我似乎加剧了。我使用organier查看文件 实际上数据丢失了。事情很多次都不起作用。我不是为什么它如此不一致。此外,如果我终止xcode中的调试器,我希望只丢失最近的更改,而不是整个文件数据似乎确实发生。考虑到可能是另一种持久存储策略,整个NSKeyedArchiv的东西看起来很好看。任何人都会遇到这样的事情。

- (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];    
}

1 个答案:

答案 0 :(得分:4)

您不需要那么多代码。保存您的数据:

NSMutableDictionary *appState = [NSMutableDictionary dictionary];
[appState setObject: self.accountList forKey: kDataKey];
[appState setObject: appDelegate.callList forKey: kCallListKey];
[appState setObject: self.phoneSettings forKey: kPhoneSettings];
if ( [NSKeyedArchiver archiveRootObject: appState toFile: [self dataFilePath]] == NO )
    NSLog(@"Failed to archive %@", appState);

阅读数据:

NSMutableDictionary *appState = [NSKeyedUnarchiver unarchiveObjectWithFile: [self dataFilePath]];

请注意,如果文件不包含有效的存档,NSKeyedUnarchiver可以引发NSInvalidArgumentException,因此您应该将该调用括在@try {}块中。