Created
May 22, 2014 12:07
-
-
Save samdods/220791a7e87895da9fec to your computer and use it in GitHub Desktop.
Use GCD dispatch_once function to execute some code only once per instance.
This file contains 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
/* | |
* This may be obvious to some, but the most common use of dispatch_once is for the initialisation of | |
* a class singleton, so some people may not know how to do it per instance. | |
* | |
*/ | |
@implementation MyViewController { | |
dispatch_once_t _oncePerInstance; | |
} | |
- (void)viewDidLayoutSubviews | |
{ | |
[super viewDidLayoutSubview]; | |
dispatch_once(&_oncePerInstance, ^{ | |
// do something after laying out subviews for the first time. | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment