Lan Yi iOS iOS single-instance destruction

Source: Internet
Author: User

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

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.