Search code examples
objective-cios5singleton

Singleton in iOS 5?


Hi I had an implementation previous versions of iOS for a singleton as follows:

.h file

@interface CartSingleton : NSObject 
{

}
+(CartSingleton *) getSingleton;

.m file

@implementation CartSingleton

static CartSingleton *sharedSingleton = nil;

+(CartSingleton *) getSingleton
{
    if (sharedSingleton !=nil)
       {
        NSLog(@"Cart has already been created.....");
        return sharedSingleton;
       }
    @synchronized(self)
   {
    if (sharedSingleton == nil)
       {
        sharedSingleton = [[self alloc]init];
        NSLog(@"Created a new Cart");
       }
   }
    return sharedSingleton;
}
//==============================================================================
+(id)alloc
{
    @synchronized([CartSingleton class])
   {
    NSLog(@"inside alloc");
    NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
    sharedSingleton = [super alloc];
    return sharedSingleton;
   }

    return nil;
}

//==============================================================================
-(id)init
{
    self = [super init];
}

However on the web I see people have implemented the Singleton design pattern using this code:

+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init]; // or some other init method
  });
  return _sharedObject;
}

Could someone who is experience please guide me. Im a newbie and thoroughly confused between the old iOS implementation of the Singleton and the new one and which is the correct one?

Thanks a lot


Solution

  • Strictly speaking, you must use:

    + (MySingleton*) instance {
         static dispatch_once_t _singletonPredicate;
         static MySingleton *_singleton = nil;
    
         dispatch_once(&_singletonPredicate, ^{
            _singleton = [[super allocWithZone:nil] init];
         });
    
         return _singleton;
     }
    
     + (id) allocWithZone:(NSZone *)zone {
          return [self instance];
     }
    

    Now you guarantee that one cannot call alloc/init and create another instance.

    Explanation: The instance method is at the class level and is your main access method to get a reference to the singleton. The method simply uses the dispatch_once() built-in queue that will only execute a block once. How does the runtime guarantee that the block is only executed once? Using the predicate you supply (of type dispatch_once_t). This low-level call will guarantee that even if there are multiple threads trying to call it, only one succeeds, the others wait until the first one is done and then returns.

    The reason we override allocWithZone is because alloc calls allocWithZone passing nil as the zone (for the default zone). To prevent rogue code from allocating and init-ializing another instance we override allocWithZone so that the instance passed back is the already initialized singleton. This prevents one from creating a second instance.