Create a singleton for iOS development

Source: Internet
Author: User
A singleton is a type. This class can only instantiate an object when it is used for the first time, and directly call this object later (a bit shared ). In the Foundation framework, for example, NSFileManger and NSNotificationCenter, they are obtained through their class methods defaultManager and defaultCenter. Although it is not a strict Singleton, these class methods return a shared instance of classes that can be accessed in all the code of the application. There has always been a lot of debate over the best way to use Objective-C to implement the singleton model, and developers seem to change their thinking every few years. They also introduced a function that is suitable for implementing the singleton mode.
This function is dispatch_once:

Void dispatch_once (dispatch_once_t * predicate, dispatch_block_t block );

This function receives a dispatch_once predicate used to check whether the code block has been scheduled (a long integer, actually used as a BOOL ). It also receives a code block that only needs to be scheduled once in the life cycle of the application. This example is used for instantiation of the shared instance. Dispatch_once not only means that the Code will be run only once, but also thread-safe, this means that you do not need to use such as @ synchronized to prevent non-synchronization when multiple threads or queues are used.
If called by multiple threads, the function is synchronized until the code block is complete.
How do I actually use this? Well, suppose there is an AccountManager class, and you want to access the shared instance of this class in the whole application. You can simply implement a class method using the following code:

+ (AccountManager *) sharedManager {

Static AccountManager * sharedAccountManagerInstance = nil;

Static dispatch_once_t predicate;

Dispatch_once (& predicate, ^ {

SharedAccountManagerInstance = [[self alloc] init];

});


Return sharedAccountManagerInstance;

}

This means that you can access the shared instance at any time. The only thing you need to do is:

AccountManager * accountManager = [AccountManager sharedManager];

In this case, you now have a shared instance in the application, which is created only once. This method has many advantages:
1 thread security 2 Good to meet static analyzer requirements 3 compatible with automatic reference count (ARC)
4. Only a small amount of code is required.
    The disadvantage of this method is that it still runs to create a non-shared instance:

    AccountManager * accountManager = [[AccountManager alloc] init];

    Sometimes you want this kind of behavior, but if you want only one instance to be instantiated, you need to pay attention to this.
    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.