我的应用程序涉及用户保存我使用 NSCoding/NSKeyedArchiver 存储的数据。我在应用程序的第一次运行时向用户提供示例数据对象。
预期的行为发生在常规测试期间以及通过临时部署。不幸的是,当应用程序通过应用商店下载时,会发生一个重大错误。
可能还有哪些其他环境考虑因素,以便我可以在常规测试中重现(然后修复)该问题?
预期行为:
除了当前的数据对象之外,新用户还可以添加/编辑数据对象。(经典的 CRUD 场景)。
实际行为:
如果用户的第一个操作是保存一个新对象,则所有先前加载的示例对象都会消失(难以捉摸的错误)。但是,如果用户的第一个操作是编辑,则所有对象都按预期保留,用户可以添加其他对象而不会出现问题。
谢谢您的帮助。
编辑
在我最近的测试中,我将构建配置切换为在“运行”方案中发布。
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;
}