Create a singleton a lot of ways. I'll start by listing an official Apple document.
[CPP]View Plaincopy
- static Accountmanager *defaultmanager = nil;
- + (Accountmanager *) Defaultmanager {
- if (! Defaultmanager) Defaultmanager = [[self allocwithzone:null] init];
- return defaultmanager;
- }
Of course, after iOS4, there is another way to do this:
[CPP]View Plaincopy
- + (Accountmanager *) Sharedmanager
- {
- static Accountmanager *sharedaccountmanagerinstance = nil;
- static dispatch_once_t predicate;
- Dispatch_once (&predicate, ^{
- Sharedaccountmanagerinstance = [[Self alloc] init];
- });
- return sharedaccountmanagerinstance;
- }
This writing comes from Objcolumnist, which has the following characteristics:
1. Thread safety.
2. Meet the requirements of the static analyzer.
3. Compatible with ARC
Then I was a little curious about the dispatch_once, this function, not seen AH.
So I went to the official documents to find out what to say.
The following is an official document introduction:
Dispatch_once
Executes a block object once and only once for the lifetime of a application.
void Dispatch_once (
dispatch_once_t *predicate,
dispatch_block_t block);
Parameters
predicate
A pointer to a dispatch_once_t structure which is used to test whether the block have completed or not.
Block
The block object to execute once.
Discussion
This function was useful for initialization of the global Data (singletons) in an application. Always call this function before using or testing any variables that is initialized by the block.
If called simultaneously from multiple threads, this function waits synchronously until the block has completed.
The predicate must a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined.
Availability
- Available in IOS 4.0 and later.
Declared in
Dispatch/once.h
We see that the function of this method is to execute and execute only one block object at a time throughout the declaration cycle of the program. It's just a single case. Moreover, some of the actions we need to initialize at the beginning of the program can be executed only once for the purpose of guaranteeing it, and also in this dispatch_once.
Then we see that it needs an assertion to determine whether this block of code is executed, the pointer to the assertion is to be saved, and a pointer to be saved in relation to the first method.
It is clear from the introduction of the method that this function is useful for creating a data object that initializes a global (singleton mode) in the application.
If you call it in multiple threads at the same time, the function waits for a synchronous wait until the block call ends.
The pointer to this assertion must be saved globally, or placed in a static zone. The results of dispatch_once execution are unpredictable with assertions stored in an auto-allocated area or dynamic region.
Summary: 1. This method can be used to create a singleton or some initialization action, to ensure its uniqueness. 2. This method is thread-safe, so be sure to use it boldly in a child thread. (If your dispatch_once_t *predicate object must be a global or static object.) This is important, and if this is not guaranteed, there is no guarantee that the method will only be executed once. )
iOS single case