The singleton pattern is one of the most commonly used methods in development, and there are two official ways to use the iOS singleton pattern, as follows:
Do not use GCD
#import " ServiceManager.h " Static ServiceManager *Defaultmanager; @implementation ServiceManager+ (ServiceManager *) defaultmanager{ if(! Defaultmanager) Defaultmanager=[[self allocwithzone:null] init]; return Defaultmanager;} @end
Using GCD
#import " @implementation ServiceManager + (servicemanager *) sharedmanager{ static dispatch_once_t predicate; static servicemanager * Sharedmanager; Dispatch_once ( &predicate, ^{sharedmanager =[[servicemanager alloc] init]; }); return Sharedmanager;} @end
Dispatch_once this function, which guarantees that a piece of code is executed only once in the entire application life cycle!
iOS Development notes-two types of singleton patterns