-
-
Save xjones/1305095 to your computer and use it in GitHub Desktop.
Macro for creating your "shared instance" using GCD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @implementation MySharedThing | |
| + (id)sharedInstance | |
| { | |
| DEFINE_SHARED_INSTANCE_USING_BLOCK(^{ | |
| return [[self alloc] init]; | |
| }); | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; \ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @lukeredpath for the original.