NSKeyedArchiver 文件 - 丢失数据

发布于 2024-08-19 08:00:12 字数 2829 浏览 3 评论 0原文

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

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

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

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

Using Iphone SDK 3.1.2. I am saving some info to a file using NSKeyedArchiver object. I have 4 keys.
I read data from the file for all 4 keys on applicationDidFinshLaunching and write back to the file (all 4 keys) on applicationDidTerminate. Also when the user saves any info i write the data for the particular key its for.

In case there is no file initially i allocate an array for the accountlist, this allows me to add accounts to it.

I am having issues with the file periodically losing the data saved to it. Sometimes its one key affected ie accountlist, sometimes it is the favorites list. It seems to be exacerbated if i just save 1key as opposed to all 4. ive viewed the file using organsier
and indeed the data is missing. The thing is many times it does not work. I don't why its so inconsistent. Also if i termintate the debugger in xcode i would expect to only lose recent changes not the whole files data whihc does seem to happen. The whole NSKeyedArchiv er thing seems so flakey im considering maybe another persistent storage strategy. Anyone come across anything like this.

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

then in

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2024-08-26 08:00:12

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

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

You don't need that much code. Save your data:

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);

Read the data:

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

Note that NSKeyedUnarchiver can raise an NSInvalidArgumentException if the file does not contain a valid archive, so you should enclose the call in a @try{} block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文