IOS Singleton Mode Learning "52 methods 6th. 45 use Dispath_once to execute thread-safe code that runs only once"

Source: Internet
Author: User
Tags gcd

Baidu definition : Single case mode is a commonly used software design mode. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system.

Wikipedia : In Software engineering, a singleton is a mathematical concept for implementing a singleton, and the instantiation of a class is limited to the design pattern of only one object.

Simple to understand : Singleton mode is only a design pattern with a strength object

This time using a singleton with GCD to create

Explain several key words

Dispatch_once : This block of code executes only once throughout the lifetime of the application and is thread-safe (if called by multiple threads, the function waits until the block of code is completed.) GCD the bottom of the process to ensure thread safety)

dispatch_once_t:(parameter) This assertion declares whether this block of code has been created.

Advantages:

(1) Dispatch_once thread safety

(2) Simple code

(3) efficient (twice times more than @synchronized "52 tips")

My usage scenario is simple, I want to create a singleton dictionary store refresh keyword to specify a refresh page with a logical change condition

The enumeration I wrote exactly used the knowledge learned in "52 tricks," because enumerations do not have to be positive integers or four characters (usually 4 characters represent a byte int is 4 bytes)

So I can define enumerations for different pages and each page has its own independent enumeration parameters.

Use:

Invoke the instance method inside the class method directly when using [Ahfrefreshpageutil shareinstance]setneedrefreshstatus:yes Type:ahfsearchrefreshtypeall];

This ensures that key-value pairs are unique

Show Create a single example:

////  ahfrefreshpageutil.h//  dailylife////  Created by HF on 16/5/3.////#import <foundation/foundation.h > @interface ahfrefreshpageutil:nsobject+ (ahfrefreshpageutil *) sharedinstance; @end

////  ahfrefreshpageutil.m//  dailylife////  Created by HF on 16/5/3.////#import "AHFRefreshPageUtil.h" @ Implementation ahfrefreshpageutil{    nsmutabledictionary *refreshdictionary;} static Ahfrefreshpageutil *refreshpageutil; -(instancetype) init{self    = [super init];    if (self) {        refreshdictionary = [nsmutabledictionary dictionary];    }    return self;} + (Ahfrefreshpageutil *) sharedinstance{    static dispatch_once_t Oncetoken;    Dispatch_once (&oncetoken, ^{        refreshpageutil = [[Ahfrefreshpageutil alloc] init];    });    return refreshpageutil;} To add a target instance method

In fact, there are potential bugs in this case, if you use

[[Ahfrefreshpageutil Alloc]init] is the normal initialization, not a singleton usage so should be alloc and copy are rewritten to prevent accidental errors, but I am here to default this usage, if there is a alloc usage (there is no similar need In the case of the request) that must have been a pig-mate, review code good to say he's fine haha here no longer add code (if you want to write the perfect can try to rewrite the Alloc copy method to let them return to the same instance "Sharedinstance")

One of my technical users summed up a very good simple sentence "mainly see how to implement the thread unique creation"

IOS Singleton Mode Learning "52 methods 6th. 45 use Dispath_once to execute thread-safe code that runs only once"

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.