Multithreading in iOS development practices (Singleton Mode)
Singleton mode: This mode ensures that there is only one instance in a class during the program running, and the instance is easy for external access, so as to conveniently control the number of instances and save system resources.
Singleton mode: Share a resource in the entire application (this resource only needs to be created and initialized once ).
I. Singleton mode-ARC:
1. retain a Global static instance in. m (static prevents other classes from referencing the modified value)
Static id _ instance;
2. Override allocWithZone: method. Create a unique instance here (pay attention to thread security)
+ (Id) allocWithZone :( struct _ NSZone *) zone {
If (_ instance = nil) {// prevents frequent locking
@ Synchronized (self ){
If (_ instance = nil) {// prevents multiple Creations
_ Instance = [super allocWithZone: zone];
}
}
}
Return _ instance;
}
3. Provides a class method to allow external access to a unique instance.
+ (Instancetype) sharedMusicTool {
If (_ instance = nil) {// prevents frequent locking
@ Synchronized (self ){
If (_ instance = nil) {// prevents multiple Creations
_ Instance = [[self alloc] init];
}
}
}
Return _ instance;
}
4. Implement copyWithZone: Method
-(Id) copyWithZone :( struct _ NSZone *) zone {
Return _ instance;
}
5. If you want to implement a hungry Singleton, the load method is implemented. (Not recommended)
// When the class is loaded to the OC Runtime Environment (memory), it will be called once (a class will only load once)
+ (Void) load {
_ Instance = [[self alloc] init];
}
Music Singleton tool example code:
MusicTool. h
# Import
@ Interface MusicTool: UIView // provides one class method for external access to the unique instance + (instancetype) sharedMusicTool; @ end
MusicTool. m
# Import "MusicTool. h "// Singleton mode: lazy @ implementation MusicToolstatic id _ instance; // static prevents other class extern references from modifying the value/*** if you want to implement a hungry Singleton, the load method is implemented * When the class is loaded to the OC Runtime Environment (memory), it will be called once (a class will only load once) * // + (void) load {/_ instance = [[self alloc] init]; //} // It is called only when this class is used for the first time. // + (void) initialize {// NSLog (@ "= initialize ="); //}/*** the alloc method calls this method internally */+ (id) allocWithZone :( struct _ NSZone *) zone {if (_ instance = nil) {// prevent frequent lock @ synchronized (self) {if (_ instance = nil) {// prevent multiple _ instance = [super allocWithZone: zone] ;}}} return _ instance ;}/ *** instantiate class */+ (instancetype) sharedMusicTool {if (_ instance = nil) {// prevents frequent shackles @ synchronized (self) {if (_ instance = nil) {// prevent multiple _ instance = [[self alloc] init] ;}}}return _ instance;}/*** rewrite the copy method, ensure that the copied file is also a singleton class */-(id) copyWithZone :( NSZone *) zone {return _ instance;} @ end
Ii. Singleton mode-non-ARC
Non-ARC (MRC), Singleton mode implementation (several more steps than ARC)
Memory Management
-(Id) retain {return self ;}
-(NSUInteger) retainCount {return 1 ;}
-(Oneway void) release {}
-(Id) autorelease {return self ;}
MusicTool. m
# Import "MusicTool. h "@ implementation MusicToolstatic id _ instance; // static prevents other class extern references from modifying the value/*** if you want to implement a hungry Singleton, the load method is implemented * When the class is loaded to the OC Runtime Environment (memory), it will be called once (a class will only load once) * // + (void) load {/_ instance = [[self alloc] init]; //} // It is called only when this class is used for the first time. // + (void) initialize {// NSLog (@ "= initialize ="); //}/*** the alloc method calls this method internally */+ (id) allocWithZone :( struct _ NSZone *) zone {if (_ instance = nil) {// prevent frequent lock @ synchronized (self) {if (_ instance = nil) {// prevent multiple _ instance = [super allocWithZone: zone] ;}}} return _ instance ;}/ *** instantiate class */+ (instancetype) sharedMusicTool {if (_ instance = nil) {// prevents frequent shackles @ synchronized (self) {if (_ instance = nil) {// prevent multiple _ instance = [[self alloc] init] ;}}}return _ instance;}/*** rewrite the copy method, make sure that the copied file is also a singleton class */-(id) copyWithZone :( NSZone *) zone {return _ instance;} // rewrite the release method, do not release-(oneway void) release {} // override the retain method and return the singleton class-(instancetype) retain {return self; // return _ instance;} // The calculation is always 1-(NSUInteger) retainCount {return 1 ;}@ end
Test:
MusicTool * mt1 = [[MusicTool alloc] init]; MusicTool * mt2 = [MusicTool sharedMusicTool]; MusicTool * mt3 = [mt2 copy]; [mt2 release]; // release NSLog in a non-arc environment (@ "=%==%=%@", mt1, mt2, mt3 );
III. The Singleton mode varies in the ARC \ MRC environment. You can use macros to determine whether the singleton mode is an ARC environment.
# If _ has_feature (objc_arc)
// ARC
# Else
// MRC
# Endif
Define a singleton as a macro and extract public classes
1. HMSingleton. h
//. Hfile # define HMSingletonH (name) + (instancetype) shared # name ;//. m file # if _ has_feature (objc_arc) # define HMSingletonM (name) \ static id _ instace; \\+ (id) allocWithZone :( struct _ NSZone *) zone \ {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^{\_ instace = [super allocWithZone: zone] ;\}); \ return _ instace; \}\+ (instancetype) shared # name \ {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ _ instace = [self alloc] init]; \}); \ return _ instace ;\}\\-(id) copyWithZone :( NSZone *) zone \{\ return _ instace ;\}# else # define HMSingletonM (name) \ static id _ instace ;\\+ (id) allocWithZone :( struct _ NSZone *) zone \{\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ _ instace = [super allocWithZone: zone] ;\}); \ return _ instace ;\}\+ (instancetype) shared # name \ {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^{\_ instace = [[self alloc] init] ;\}); \ return _ instace; \}\- (id) copyWithZone :( NSZone *) zone \{\ return _ instace ;\}\\-(oneway void) release {}\-(id) retain {return self ;}\-(NSUInteger) retainCount {return 1 ;}\-(id) autorelease {return self ;}# endif
2. To use this macro in the entire project, import the header file in the pre-compiled pch file.
# Ifdef _ OBJC __
# Import
# Import
# Import "HMSingleton. h"
# Endif
3. Use the HMSingleton tool class to implement the HMMusicTool Singleton
HMMusicTool. h
# Import
@ Interface HMMusicTool: NSObject // macro-defined method HMSingletonH (MusicTool) @ end
HMMusicTool. m
# Import "HMMusicTool. h" @ implementation HMMusicTool // macro-defined method HMSingletonM (MusicTool) @ end
4. test:
HMMusicTool *tool1 = [HMMusicTool sharedMusicTool]; HMMusicTool *tool2 = [HMMusicTool sharedMusicTool]; HMMusicTool *tool3 =[[HMMusicTool alloc]init]; HMMusicTool *tool4 =[tool3 copy]; NSLog(@"%@", tool1); NSLog(@"%@", tool2); NSLog(@"%@", tool3); NSLog(@"%@", tool4);