iOS Singletons

ios3

Suppose that… for whatever reason, you need to target iOS 3.x. Yes. You need to build an app that should run on any possible device. Or say that… you simply don’t wanna use GCD.

What’s the alternative to write a singleton?
[cc lang=”objc”]
+ (id)sharedInstance
{
if(_instance = nil)
{
@synchronized(self)
{
if(_instance == nil)
{
_instance = [[[self class] alloc] init];
}
}
}
return _instance;
}[/cc]
This is a nice alternative to the GCD option. The first time the instance is created, you’ll suffer the lag caused by the @synchronized block. But after that. it’s just an if. No context switch. No whatsoever! REALLY performant!

%d