I've got a weird issue here. I'm creating a game in Sprite Kit and when I first initialize all the variables it seems to work fine. I have a singleton class that holds all the data that I will use in the game. This singleton holds one object (the World) and the World object holds arrays of objects that I use throughout the game.
After initialization, I try to print out what the World object contains in a certain array. Below is an example:
NSLog(@"Game manager ports: %@", [IPGameManager sharedGameData].world.ports);
However, when this is run, the simulator hangs. I haven't tried it on a real device yet (don't have the developer account yet). However, when I print the same thing right after the initialization of the World 'ports', it prints fine. Here's the header file of the World object:
@interface IPWorld : NSObject <NSCoding>
@property (nonatomic, retain) IPPlayer* player;
@property (nonatomic, retain) NSMutableArray* ports;
@property (nonatomic, retain) NSMutableArray* crewMembers;
@property (nonatomic, retain) NSMutableArray* ships;
@property (nonatomic, retain) NSMutableArray* quests;
@end
I wouldn't think the 'retain' or anything would cause this but I am not sure what the issue is. Any help is greatly appreciated. Thanks!
EDIT:: Below is the code of my singleton:
+(instancetype)sharedGameData {
NSLog(@"Loading sharedGameData");
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"should only see this once");
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
+(instancetype)loadInstance {
NSData* decodeData = [NSData dataWithContentsOfFile:[IPGameManager filePath]];
if (decodeData) {
IPGameManager* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodeData];
return gameData;
}
return [[IPGameManager alloc] init];
}
+(NSString*)filePath {
static NSString* filePath = nil;
if (!filePath) {
filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"gamedata"];
}
return filePath;
}