IOS Development notes-singleton Mode

Source: Internet
Author: User

IOS Development notes-singleton Mode

I transferred from java to IOS and developed it. I learned some of the skills used in the project by myself in concert with the actual project. The APP is relatively small, and the knowledge involved is relatively simple. I don't need to read it anymore. I will discuss it with you. The Singleton mode ensures that a class has only one instance while the program is running, and the account management of the company's APP is implemented through Singleton. First run the Code:

@ Interface JVAccountManager: NSObject
+ (JVAccountManager *) sharedAccount;
@ End

@ Implementation JVAccountManager
+ (JVAccountManager *) sharedAccount
{
Static JVAccountManager * sharedAccountManager;
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
SharedAccountManager = [[JVAccountManager alloc] init];
});
Return sharedAccountManager;
}
@ End

This is our company's implementation method. We can find other implementation methods online, but we need to rewrite some methods to ensure the uniqueness of the instance, such as allocWithZone and copyWithZone. We will not repeat them here, I posted the simplest implementation method of the Code;
The stranger is the dispatch_once method, which is actually to ensure the uniqueness of the instance. This method is provided in display_once and GCD (Grand Central Dispatch). According to the explanation in this book of IOS programming, it is a low-level simulation of NSOperation and NSOperationQueue, use Objective-C block. In fact, I am not very clear about this part. I will analyze it later. It must be understood that the code block in the dispach_once function will only be executed once, and it is thread-safe.
Void dispatch_once (dispatch_once_t * predicate, dispatch_block_t block); the first parameter, predicate, is the predicate used to check whether the code block represented by the second parameter is called, the second parameter is the code block that will only be called once in the entire application;

The method to obtain this unique instance in this program: JVAccountManager * sharedAccountManager = [JVAccountManager sharedAccount];

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.