Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCoding and saving/loading default values issue

I'm writing some code for an iPhone app, and I'm having issues getting default data to load in correctly. I am basing my code off some example from the "Learning Cocos2d" book by Ray Wenderlich.

It seems that even when I delete the app outright and try to start from fresh data that the app inconsistently either doesn't try to load the data, or incorrectly thinks that there is data, and loads null.

I'm using containsValueForKey to check if a value exists and then load it or load some default value, but even on a fresh installation the containsValueForKey finds data and doesn't load the defaults. In xcode's organizer I checked my device's file structure and the Documents folder, where I specified to save, doesn't look like it contains any files, so I'm not sure what it's grabbing.

My guess is that the problem is something to do with the initWithCoder function. It seems to mysteriously go through the function sometimes, but not all the time. Another weird thing is that I call [[GameManager sharedGameManager] save] when the player gets a highscore (not shown here, but the code is the exact same as this objectiveList, only an int) and it appears to save it correctly.

And now the code:

GCDatabase.h

#import <Foundation/Foundation.h>
id loadData(NSString * filename);
void saveData(id theData, NSString *filename);

GCDatabase.m

#import "GCDatabase.h"
NSString * pathForFile(NSString *filename) {
// 1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
// 2
NSString *documentsDirectory = [paths objectAtIndex:0];
// 3
return [documentsDirectory stringByAppendingPathComponent:filename];
}

id loadData(NSString * filename) {

NSString *filePath = pathForFile(filename);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

    NSData *data = [[[NSData alloc] initWithContentsOfFile:filePath] autorelease];
    NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
    id retval = [unarchiver decodeObjectForKey:@"Data"];
    [unarchiver finishDecoding];
    return retval;
}
return nil;
}

void saveData(id theData, NSString *filename) {

NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
[archiver encodeObject:theData forKey:@"Data"];
[archiver finishEncoding];
[data writeToFile:pathForFile(filename) atomically:YES];
}

GameManager.h

@interface GameManager : NSObject <NSCoding>{
NSMutableArray *objectiveDescriptions;
}
@property (nonatomic, retain) NSMutableArray * objectiveDescriptions;
+(GameManager*)sharedGameManager;
-(void)save;
-(void)load;
-(void)encodeWithCoder:(NSCoder *)encoder;
-(id)initWithCoder:(NSCoder *)decoder;

@end

GameManager.m (I added the load function, in an attempt to force it to load, but it doesn't seem to work)

+(GameManager*)sharedGameManager {
@synchronized([GameManager class])
{
    if(!sharedGameManager) {
        sharedGameManager = [loadData(@"GameManager") retain];
        if (!sharedGameManager) {
            [[self alloc] init];
        }
    }
    return sharedGameManager;
}
return nil; 
}

+(id)alloc {
@synchronized([GameManager class]){
    NSAssert(sharedGameManager == nil, @"Attempted to allocate a second instance of the Game Manager singleton");
    sharedGameManager = [super alloc];
    return sharedGameManager;
}
return nil;
}

- (void)dealloc {
[objectiveList release];
[super dealloc];
}

- (void)save {
saveData(self, @"GameManager");
}

-(void)load {
loadData(@"GameManager");
}

- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:objectiveList forKey:@"objectiveList"];
}

- (id)initWithCoder:(NSCoder *)decoder  {
self = [super init];
if (self != nil) {
if ([decoder containsValueForKey:@"objectiveList"]) {
        objectiveList = [decoder decodeObjectForKey:@"objectiveList"];
    } else {
        [objectiveList addObjectsFromArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];
    }
}

return self;
}
@end
like image 642
Mark Tuttle Avatar asked Nov 04 '22 00:11

Mark Tuttle


2 Answers

I have not read your full code.. But I found a problem in code....

You have not allocated memory to objectiveList array.. Unless and until you allocate memory to array, objects will not be added...

I think go for

 objectiveList = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];

instead of

    [objectiveList addObjectsFromArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];

Check for the syntax.. Hope this may help as it troubled me also in the past where I forgot to allocate memory to the array.. And kept on adding objects resulting in null... :) In case it doesn't solve your problem, I'll look for code later completely.. :)

like image 84
Nikhil Aneja Avatar answered Nov 15 '22 05:11

Nikhil Aneja


I seem to see the problem. When the constructor is called the first time, the objectiveList is not even created as the "initWithCoder" is never called. You have to override the init method as well in order for the objectiveList array to be constructed. Basically, the code that is calling the init method is in here:

+(GameManager*)sharedGameManager {
@synchronized([GameManager class])
{
    if(!sharedGameManager) {
        sharedGameManager = [loadData(@"GameManager") retain];
        if (!sharedGameManager) {
            [[self alloc] init]; // GOES INTO INIT METHOD, NOT INITWITHCODER!
        }
    }
    return sharedGameManager;
}
return nil; 
}

On a side note, that singleton implementation gave me a headache. Just saying. :)

like image 24
Andres C Avatar answered Nov 15 '22 05:11

Andres C