开发者

Random data loss on iOS after restarting app

Clearly I must not be doing something right but this happens on random occasions. I can't make the issue appear by following certain steps so it has become extremely hard to debug. I have an app in which every time an object is added, or deleted it writes the file to a plist. I can verify the plist in the simulator and I also have an NSLog each time my save function is called. The data is loaded when applicationWillEnterForeground is called, which is also working correctly. On certain occasions after starting up the app it will revert to the previous save before the most recent changes. Does iOS cache data files? If it tries to load a file from disk to an array, could it possible already have the previous load in a cache and create the array with that data instead?

Save method:

- (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
}

Load method; I just added the if (self.aquariums == null) check and it might have solved the issue but it's hard for me to confirm since I can't recreate the problem:

- (void)applicationDidBecom开发者_JS百科eActive:(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];
        }
}
}


After releasing and not getting any reports of loss of data the issue must definitely have been

if (self.aquariums == NULL) {

so people remember if you are going to reload data when an app returns from being in the background set it to null first and release

aquariums = NULL;
[aquariums release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜