1

我的应用程序涉及用户保存我使用 NSCoding/NSKeyedArchiver 存储的数据。我在应用程序的第一次运行时向用户提供示例数据对象。

预期的行为发生在常规测试期间以及通过临时部署。不幸的是,当应用程序通过应用商店下载时,会发生一个重大错误。

可能还有哪些其他环境考虑因素,以便我可以在常规测试中重现(然后修复)该问题?

预期行为:

除了当前的数据对象之外,新用户还可以添加/编辑数据对象。(经典的 CRUD 场景)。

实际行为:

如果用户的第一个操作是保存一个新对象,则所有先前加载的示例对象都会消失(难以捉摸的错误)。但是,如果用户的第一个操作是编辑,则所有对象都按预期保留,用户可以添加其他对象而不会出现问题。

谢谢您的帮助。

编辑

在我最近的测试中,我将构建配置切换为在“运行”方案中发布。

http://i.imgur.com/XNyV6.png

App Delegate,正确初始化应用程序

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.dataArray = nil;
    self.dataArray = [AppDelegate getArray];
    if (self.dataArray == nil) {
        self.dataArray = [[NSMutableArray alloc] init];
    }

    //First run of the app
    if (dataArray.count == 0) {
        //Add sample data to array
        //Save array
        NSString *path = [AppDelegate getDocPath];
        [NSKeyedArchiver archiveRootObject:self.dataArray toFile:path];
    }
}

+(NSString *) getDocPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *tempDocPath = [documentsPath stringByAppendingPathComponent:@"FilePath.dat"];
    return tempDocPath; 
}

+(NSMutableArray *)getArray {
    return [[NSKeyedUnarchiver unarchiveObjectWithFile:[AppDelegate getDocPath]] mutableCopy];
}

对象创建,如果尚未编辑数据,则会删除预加载的数据

-(void)viewDidLoad {
    tempArray = nil;
    tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[AppDelegate getDocPath]];
    if (tempArray == nil) {
        tempArray = [[NSMutableArray alloc] init];  
    }   
}

-(void)saveObject {
    [tempArray addObject:createdData];
    [tempArray sortUsingSelector:@selector(compare:)];
    NSString *path = [AppDelegate getDocPath];
    [NSKeyedArchiver archiveRootObject:tempArray toFile:path];
    AppDelegate *dg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    dg.dataArray = tempArray;
}
4

2 回答 2

2

我不确定如何解决您当前的问题(不查看代码),但您可以在未来避免它:

确保您提交给应用商店的构建是您已进行 QA 的临时构建,但使用应用商店配置文件签名。

Two advantages: 1) You should be able to repro the same bug on the adhoc and appstore build 2) dSym for both these are the same. So, you dont have to wait to get the AppStore crash logs before you can dig in and see what's happening.

于 2012-08-30T15:47:14.710 回答
1

我想在保存新对象时,您不会将其附加到现有数据中。您可能正在覆盖先前创建的文件。您应该访问前一个文件并将新数据附加到前一个文件中。共享代码将有助于指出您哪里出错了。

编辑:替换以下代码并检查它是否仍然显示相同的行为

-(void)viewDidLoad {
    tempArray = nil;
    tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[AppDelegate getDocPath]mutableCopy];
    if (tempArray == nil) {
        NSLog(@"tempArray is nil"); //if tempArray doesn't get initialized by the file contents  
        tempArray = [[NSMutableArray alloc] init];

    }   
}
于 2012-08-30T15:46:54.363 回答