Skip to content

Instantly share code, notes, and snippets.

@samdods
Created May 22, 2014 12:07
Show Gist options
  • Save samdods/220791a7e87895da9fec to your computer and use it in GitHub Desktop.
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 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