Why isnt my data persisting with nskeyedarchiver?

Posted by aking63 on Stack Overflow See other posts from Stack Overflow or by aking63
Published on 2012-12-18T23:01:32Z Indexed on 2012/12/18 23:02 UTC
Read the original article Hit count: 184

Im just working on what should be the "finishing touches" of my first iPhone game. For some reason, when I save with NSKeyedArchiver/Unarchiver, the data seems to load once and then gets lost or something. Here's what I've been able to deduce:

  1. When I save in this viewController, pop to the previous one, and then push back into this one, the data is saved and prints as I want it to.

  2. But when I save in this viewController, then push a new one and pop back into this one, the data is lost.

Any idea why this might be happening? Do I have this set up all wrong? I copied it from a book months ago. Here's the methods I use to save and load.

    - (void) saveGameData {
        NSLog(@"LS:saveGameData");

        // SAVE DATA IMMEDIATELY
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
        NSMutableData *gameSave= [NSMutableData data];
        NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameSave];
        [encoder encodeObject:categoryLockStateArray forKey:kCategoryLockStateArray];
        [encoder encodeObject:self.levelsPlist forKey:@"levelsPlist"];

        [encoder finishEncoding];
        [gameSave writeToFile:gameStatePath atomically:YES];

        NSLog(@"encoded catLockState:%@",categoryLockStateArray);
    }

- (void) loadGameData {
    NSLog(@"loadGameData");

    // If there is a saved file, perform the load
    NSMutableData *gameData = [NSData dataWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"gameState.dat"]];

    // LOAD GAME DATA
    if (gameData) {
        NSLog(@"-Loaded Game Data-");
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
        self.levelsPlist = [unarchiver decodeObjectForKey:@"levelsPlist"];
        categoryLockStateArray = [unarchiver decodeObjectForKey:kCategoryLockStateArray];

        NSLog(@"decoded catLockState:%@",categoryLockStateArray);
    }

    // CREATE GAME DATA
    else {
        NSLog(@"-Created Game Data-");
        self.levelsPlist = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLevelsPlist ofType:@"plist"]];
    }

    if (!categoryLockStateArray) {
        NSLog(@"-Created categoryLockStateArray-");

        categoryLockStateArray = [[NSMutableArray alloc] initWithCapacity:[[self.levelsPlist allKeys] count]];

        for (int i=0; i<[[self.levelsPlist allKeys] count]; i++) {
            [categoryLockStateArray insertObject:[NSNumber numberWithBool:FALSE] atIndex:i];
        }
    }

    // set the properties of the categories
    self.categoryNames = [self.levelsPlist allKeys];
    NUM_CATEGORIES = [self.categoryNames count];
    thisCatCopy = [[NSMutableDictionary alloc] initWithDictionary:[[levelsPlist objectForKey:[self.categoryNames objectAtIndex:pageControl.currentPage]] mutableCopy]];
    NUM_FINISHED = [[thisCatCopy objectForKey:kNumLevelsBeatenInCategory] intValue];
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios