重启应用后iOS上的随机数据丢失

时间:2011-10-01 23:46:57

标签: ios objective-c iphone xcode load

显然,我一定不能做正确的事,但这种情况发生在随机的场合。我不能通过遵循某些步骤来显示问题,因此调试变得非常困难。我有一个应用程序,每次添加或删除它时,它会将文件写入plist。我可以在模拟器中验证plist,每次调用save函数时我都有一个NSLog。调用applicationWillEnterForeground时会加载数据,这也正常工作。在启动应用程序后的某些情况下,它将在最近的更改之前恢复到之前的保存。 iOS是否缓存数据文件?如果它试图将文件从磁盘加载到数组,是否可能已经在缓存中加载了先前的加载并使用该数据创建数组?

保存方法:

- (void)saveFile {
    // saves data to file

    NSLog(@"save file reached");
#ifdef LITE_VERSION

    [self.aquariums writeToFile:[self dataFilePathLite] atomically:YES];

#else

    [self.aquariums writeToFile:[self dataFilePath] atomically:YES];

#endif
}

加载方法;我刚刚添加了if (self.aquariums == null)检查,它可能已经解决了问题,但我很难确认,因为我无法重新创建问题:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */

    NSLog(@"applicationDidBecomeActive called");

if (self.aquariums == NULL) {
    NSLog(@"aquariums null, loading file");

    #ifdef LITE_VERSION
        NSString *filePath = [self dataFilePathLite];
    #else
        NSString *filePath = [self dataFilePath];
    #endif

        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
            self.aquariums = array;
            [array release];

            [self update21];

        } else {

    #ifdef LITE_VERSION
            [self convertFileName];

    #else
            [self update20];
    #endif
        }

        application.applicationIconBadgeNumber = 0;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            aquaPlannerAppDelegate_iPad *appDelegate = [[UIApplication sharedApplication] delegate];
            [appDelegate.rootView viewDidAppear:YES];
        }
}
}

1 个答案:

答案 0 :(得分:0)

在发布并且没有收到任何数据丢失报告后,问题肯定是

if (self.aquariums == NULL) {

所以人们记得,当应用程序从后台返回时,如果要重新加载数据,请先将其设置为null并释放

aquariums = NULL;
[aquariums release];