简体   繁体   English

writeToFile:atomically仅在第一次使用

[英]writeToFile:atomically only works first time

When my app starts, looks if a plist exists, if it doesn't it creates it. 当我的应用启动时,查看plist是否存在,如果不存在,它将创建它。

NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"history.plist"];
    success = [fileManager fileExistsAtPath:filePath];
    if (success) {
        NSArray *arr = [[NSArray alloc] initWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"history.plist"]];
        for (NSDictionary *par in arr) {
            [self.history addObject:[[Paradero alloc] initWithDictionary:par]];
        }
    } else {
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }

Now, when the user closes the app, to provide persistance I save the data to that file 现在,当用户关闭应用程序时,为了提供持久性,我将数据保存到该文件中

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"history.plist"];
    NSLog(@"%@", filePath);
    NSMutableArray *temparray = [[NSMutableArray alloc] initWithCapacity:5];
    for (Paradero *parada in self.history) {
        [temparray addObject:[parada asDictionary]];
    }
    NSLog(@"%@", temparray);
    NSLog(@"%c",[temparray writeToFile:filePath atomically:NO]);

    NSLog(@"yei");
}

The object is converted to a Dictionary with NSString, NSArrays so it can be saved to the file. 该对象使用NSString NSArrays转换为Dictionary,因此可以将其保存到文件中。 The first time this ever happens, that means, the file was created, nothing on it, it works just fine, but when is time to save new data, it fails, no reason given. 这是第一次发生,这意味着文件已创建,没有任何内容,可以正常工作,但是当需要保存新数据时,它会失败,没有给出任何原因。

Solved. 解决了。 For the record: The first issue was that i was testing on iOS5 Simulator, well, i don't know how to fix it there yet. 作为记录:第一个问题是我正在iOS5 Simulator上进行测试,好吧,我还不知道如何修复它。

The real issue was because there was some NSNull around, so, I just needed to turn them into empty strings. 真正的问题是因为周围有一些NSNull,所以,我只需要将它们转换为空字符串即可。

Remember to check for NSNulls, they are really a pain if they turn out from a web service. 记住要检查NSNull,如果它们来自Web服务,它们确实很痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM