public
Created — forked from lukeredpath/ExampleClass.m

Macro for creating your "shared instance" using GCD

  • Download Gist
ExampleClass.m
Objective-C
1 2 3 4 5 6 7 8 9 10
@implementation MySharedThing
 
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
 
@end
GCDSingleton.h
Objective-C
1 2 3 4 5 6 7
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject; \

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.