【问题标题】:iPhone save data getting deleted on upgrade?iPhone保存数据在升级时被删除?
【发布时间】:2011-03-29 23:36:14
【问题描述】:

我在应用商店中有一个应用,刚收到用户的消息,告诉我升级到更新版本的应用后,他们保存的数据全部消失了。

是否有明显错误的方式在 iPhone 上保存数据,从而在将应用升级到新版本时导致数据丢失?

这是我用来加载和保存的代码,它在开发机器上运行良好。这是错误的做法吗?

- (bool) saveData:(NSData*) data toFile:(NSString*) filename
{
    NSError* error;

    NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
    NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString* filePath = [docsDir stringByAppendingPathComponent:filename];
    NSString* validMarkerPath = [docsDir stringByAppendingPathComponent:[filename stringByAppendingString:@".val"]];
    NSString* backupPath = [docsDir stringByAppendingPathComponent:[filename stringByAppendingString:@".bak"]];

    // If the file exists and is marke valid, copy it over the backup.
    if([fileMgr fileExistsAtPath:filePath] && [fileMgr fileExistsAtPath:validMarkerPath])
    {
        if([fileMgr fileExistsAtPath:backupPath])
        {
            if(![fileMgr removeItemAtPath:backupPath error:&error])
            {
                NSLog(@"Error: SafeFileManager: Could not remove backup file %@: %@", backupPath, [error localizedDescription]);
            }
        }

        if(![fileMgr moveItemAtPath:filePath toPath:backupPath error:&error])
        {
            NSLog(@"Error: SafeFileManager: Could not move %@ to %@: %@", filePath, backupPath, [error localizedDescription]);
        }
    }

    // Remove the "valid" marker file, if present.
    if([fileMgr fileExistsAtPath:validMarkerPath])
    {
        if(![fileMgr removeItemAtPath:validMarkerPath error:&error])
        {
            NSLog(@"Error: SafeFileManager: Could not remove validation file %@: %@", validMarkerPath, [error localizedDescription]);
        }
    }

    // Save the new file.
    if(![data writeToFile:filePath options:NSAtomicWrite error:&error])
    {
        NSLog(@"Error: SafeFileManager: Could not save to %@: %@", filePath, [error localizedDescription]);
        return NO;
    }

    // If we succeeded, save the "valid" marker file.
    NSData* markerData = [NSData dataWithBytes:"0" length:1];
    if(![markerData writeToFile:validMarkerPath options:NSAtomicWrite error:&error])
    {
        NSLog(@"Error: SafeFileManager: Could not save validation file %@: %@", validMarkerPath, [error localizedDescription]);
        return NO;
    }
    return YES;
}

- (NSData*) loadDataFromFile:(NSString*) filename
{
    NSError* error;

    NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease];
    NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString* filePath = [docsDir stringByAppendingPathComponent:filename];
    NSString* validMarkerPath = [docsDir stringByAppendingPathComponent:[filename stringByAppendingString:@".val"]];

    // If the file isn't valid, we'll try to load the backup.
    if(![fileMgr fileExistsAtPath:validMarkerPath])
    {
        filePath = [docsDir stringByAppendingPathComponent:[filename stringByAppendingString:@".bak"]];
    }

    NSData* data = nil;

    @try
    {
        data = [NSData dataWithContentsOfFile:filePath options:NSUncachedRead error:&error];
        if(nil == data)
        {
            NSLog(@"Error: SafeFileManager: Could not load from %@: %@", filePath, [error localizedDescription]);
        }
    }
    @catch (NSException * e)
    {
        NSLog(@"Error: SafeFileManager: Could not load from %@: %@", filePath, [e description]);
        data = nil;
    }

    return data;
}

【问题讨论】:

    标签: iphone load save upgrade


    【解决方案1】:

    为了回答我自己的问题,这段代码确实有一个写入漏洞,其中备份文件和标记文件被删除,这会导致加载失败。

    我现在选择了一种更简单、甚至更暴力的方法,即简单地保存文件,然后保存备份。在加载时,它会尝试主文件,如果发生任何错误,它会使用备份文件尝试相同的加载例程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-30
      • 2013-04-23
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      相关资源
      最近更新 更多