IOS Singleton destruction
1. For the implementation of Singleton, see a previous article titled iOS Singleton quick implementation.
static InstanceClass *instance; + (InstanceClass *)defaultInstance{ @synchronized (self){ if (instance == nil) { instance = [[InstanceClass alloc] init]; } } return instance;}
2. Concepts of Singleton
The Singleton Mode means that there is only one instance. The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. This class is called a singleton class.
3 destruction ticket
I have a requirement today. I need to switch the database files when switching users, but I obtained them through a singleton. So I want to avoid destroying the current Singleton when switching users, after logging on to a new user, the singleton will be reinitialized. See the Code:
static LKDBHelper* db;
+ (LKDBHelper *) getUsingLKDBHelper {@ synchronized (self) {if (db = nil) {NSString * dbpath = [GMFileHelper getUserDbPath]; // different database paths such as multi-user database liclicare obtained when the user switches to the user. db FanBingbing. db = [[LKDBHelper alloc] initWithDBPath: dbpath] ;}} return db ;}
// Destroy the singleton upon logout. When logging on to a new user, the singleton will reinitialize + (void) attemptDealloc {db = nil ;}
In practice, it is feasible to destroy the singleton as long as you exit the logon operation. This ensures that the Singleton is reinitialized when you log on to another user. In some sense, it is no longer like a singleton. Easy to understand. If you have any mistakes, please point them out. Thank you.