Singleton mode for iOS (singleton)

Source: Internet
Author: User

Since design patterns have emerged, disputes about singleton patterns have persisted. Most of the time we need a global thing, to ensure that there is only one copy, this time the single case is the best choice, but in the multi-threaded environment also need to do a good job of thread protection.

UIApplication and Nsfilemanager under iOS are good examples-we always need to use singleton mode. But to write the code is worth a bit of scrutiny:

The simplest example is as follows, assuming we have a testclass class that needs to implement a singleton:

+ (ID) sharedinstance {    static testclass *sharedinstance = nil;    if (!sharedinstance) {        sharedinstance = [[Self alloc] init];    }    return sharedinstance;}
of course, you can see at a glance that there is no thread-safe issue at all, let's add a thread lock.

+ (ID) sharedinstance {    static testclass *sharedinstance = nil;    @synchronized (self) {        if (!sharedinstance) {            sharedinstance = [[Self alloc] init];        }    }    return sharedinstance;}
This is a very common way of writing, if you continue, you need to overload retaincount,retain, and so on, this Apple official document has discussed, even has the example.

However, after the launch of GCD, there is a dispatch_once method that can be simpler to implement in a single instance, and the Dispatch_once function prototype is as follows:

void Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);

We can see that this function receives a dispatch_once_t parameter and a block parameter. For a given predicate, the function guarantees that the relevant block is bound to execute and executes only once, and most importantly-the method is fully thread-safe. It is important to note that for blocks that need to be executed only once, the incoming predicate must be identical, so predicate is often decorated with static or global.

+ (ID) sharedinstance {    static testclass *sharedinstance = nil;    static dispatch_once_t once;    Dispatch_once (&once, ^{        sharedinstance = [[Self alloc] init];    });    return sharedinstance;}

The benefits of this writing are obvious: The code is simple and clear, and thread-safe. In addition, the dispatch_once is efficient and does not implement a heavy-weight synchronization mechanism, so the efficiency of the implementation is also high.


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.