ImageStore init method seems to have changed unexpectedly


#1

When we first create ImageStore (in Chapter 13), the book tells to implement init like this (on page 233):

- (id)init
{
    if (defaultImageStore) {
        return defaultImageStore;
    }

    self = [super init];
    if (self) {
        dictionary = [[NSMutableDictionary alloc] init];
    }

    return self;
}

Ok, fine. We don’t see this method again until Chapter 15: Saving, Loading, and Multitasking. In the section “More on Low-Memory Warnings” (page 271), we’re told to update init to register the imageStore as an observer of the UIApplicationDidReceiveMemoryWarningNotification notification. No problem; the new code in bold makes sense. But the non-bold, presumably old code doesn’t match the code above:

- (id)init
{
    if (defaultImageStore) {
        [self dealloc];
        return defaultImageStore;
    }

    defaultImageStore = self;
    dictionary = [[NSMutableDictionary alloc] init];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(clearCache:)
               name:UIApplicationDidReceiveMemoryWarningNotification
             object:nil];

    return self;
}

First, from a book-logistical point of view: When were we supposed to make these changes?

But more importantly, there are some things about this implementation of init that I don’t understand:

  1. If defaultStore exists, then we call [self dealloc]. I don’t understand this: what does it mean for an object to dealloc itself? How can it then proceed to do anything else?
  2. This implementation never calls [super init]. When is [super init] supposed to be called, and if we’re not supposed to call [super init] at some point, how can we get away with that?
  3. What was wrong with the old code, and why is this better?

I’m sure this all has to do with the intricacies of creating a Singleton, but I’m confused by the subtleties going on here.


#2

Sorry about this. We changed the implementation of the singleton and forgot to update this piece of code.

It should look like this:

- (id)init
{
    if (defaultImageStore) {
        return defaultImageStore;
    }

    self = [super init];
    if (self) {
        dictionary = [[NSMutableDictionary alloc] init];
[b]
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self
           selector:@selector(clearCache:)
               name:UIApplicationDidReceiveMemoryWarningNotification
             object:nil];[/b]


    }

    return self;
}
  1. In our old singleton implementation, if we tried to create a new instance, we’d just destroy the new instance and return the old one in the init method.

  2. The old implementation also called init somewhere else I think. I don’t remember.

  3. The version I’ve posted here (the correct version) and the one you implemented in the previous chapter is better. The one in question here is the wrong one and is “worse”.


#3

Thanks, Joe, that clears things up. Appreciate it.