IOS Development notes-singleton mode, ios development
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];