3

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

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

我遇到文件定期丢失保存到其中的数据的问题。有时它的一个关键影响即帐户列表,有时它是收藏夹列表。如果我只保存 1 个密钥而不是所有 4 个密钥,这似乎会加剧。我使用organier 查看了文件,实际上数据丢失了。事情是很多时候它不起作用。我不明白为什么它如此不一致。此外,如果我终止 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];    
}
4

1 回答 1

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{} 块中。

于 2010-01-28T16:39:38.520 回答