Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to do the loading in the singleton itself what is going on here is you create the single, you assign an lval to the singleton, you then create a new object and reassign the lval to that new object WITHOUT modifying the singleton. In other words:</p> <pre><code>//Set venue to point to singleton Venue *venue = [Venue sharedVenue]; //Set venue2 to point to singleton Venue *venue2 = [Venue sharedVenue]; NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //Set venue to unarchived object (does not change the singleton or venue2) venue = [unarchiver decodeObjectForKey:@"Venue"]; [unarchiver finishDecoding]; </code></pre> <p>What you want to do is deal with this in the sharedVenue. There are a couple of ways people do singletons, so I can't be sure what you are doing, but lets assume sharedVenue currently looks something like this:</p> <pre><code>static Venue *gSharedVenue = nil; - (Venue *) sharedVenue { if (!gSharedVenue) { gSharedVenue = [[Venue alloc] init]; } return gSharedVenue; } </code></pre> <p>Assuming that is the case you want to change it to load the object into the global backing the singleton:</p> <pre><code>static Venue *gSharedVenue = nil; - (Venue *) sharedVenue { if (!gSharedVenue) { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [data release]; gSharedVenue = [unarchiver decodeObjectForKey:@"Venue"]; [unarchiver finishDecoding]; [unarchiver release]; } if (!gSharedVenue) { gSharedVenue = [[Venue alloc] init]; } return gSharedVenue; } </code></pre> <p>Obviously you need to somehow convey the actual path to archived object file.</p> <p>EDIT BASED ON COMMENT:</p> <p>Okay, if you are using the alloc based singleton you need to deal with this in the classes init method:</p> <pre><code>- (id) init { self = [super init]; if (self) { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [data release]; Venue *storedVenue = [unarchiver decodeObjectForKey:@"Venue"]; [unarchiver finishDecoding]; [unarchiver release]; if (storeVenue) { [self release]; self = [storedVenue retain]; } } return self; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload