Skip to content

Instantly share code, notes, and snippets.

@ahmattox
Created July 30, 2012 19:18
Show Gist options
  • Save ahmattox/3209322 to your computer and use it in GitHub Desktop.
Save ahmattox/3209322 to your computer and use it in GitHub Desktop.
GCD Shared Instance
#import <Foundation/Foundation.h>
@interface SharedObject : NSObject
+ (SharedObject *) sharedInstance;
@end
#import "SharedObject.h"
@implementation SharedObject
+ (SharedObject *) sharedInstance {
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment