1 implementation of a single case see previous written article Quick implementation of iOS single example
Static Instanceclass *instance; + (Instanceclass *) defaultinstance{ @synchronized (self) { if (instance = = Nil) { instance = [[Instanceclass ALLOC] init]; } } return instance;}
2 A review of the concept of a single case
The singleton pattern means that there is only one instance. The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. This class is called a singleton class.
3 Destruction of single cases
Today we have a need to switch the database file when switching users, and I get through a singleton, so I would like to be able to switch users to destroy the current singleton, after logging into the new user will be re-initialization of the Singleton. See Code:
static lkdbhelper* db;
+ (Lkdbhelper *) getusinglkdbhelper{@synchronized (self) {if (db = = nil) {Nsstring*dbpath = [gmfilehelper Getuserdbpath]; /user Switch User time to take different db path such as multiuser database lichen.db fanbingbing.db db = [[Lkdbhelper Alloc]initwithdbpath:dbpath]; }} return db; }
Practice is feasible, as long as there is an exit login operation, the destruction of the singleton, guaranteed to log on to another user, the singleton reinitialization. In some sense, it's not like a single case. Plain cognition, there are mistakes to be welcomed to point out. Thank you.
Lan Yi iOS iOS single-instance destruction