views:

348

answers:

1

Update: This works if I call archiveRootObject: from applicationDidFinishLaunching:. If I call it from the init: method of a singleton class, it returns nil.

I'm very confused by the behavior of NSKeyedUnarchiver unarchiveObjectWithFile:. The documentation says that it will return nil if the file doesn't exist. With one of my objects, the following happens:

Game *g1 = [Game getGame];
NSString *archivePath = [Game getArchivePath];
bool success = [NSKeyedArchiver archiveRootObject:g1 toFile:archivePath];
Game *g2 = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

// success is true, g2 is nil

I've verified that the file actually does exist and is getting written to by the archiveRootObject: method. What am I doing wrong preventing me from getting a Game object back out of the archive?

A: 

When is your singleton's init method called, and who calls it? If it's called from a thread that doesn't have an NSAutoreleasePool, that could cause this kind of problem.

Darren
The first UIView that is shown calls [Game sharedGame], which is a static method that will call the init method the first time it is called. I believe an NSAutoreleasePool is available there, right?
Algorithmic