IOS Singleton destruction

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.