IOS Singleton mode Dispatch_once
Some variables need to be initialized only once (such as reading configuration parameters from a file, reading the device model, etc.), you can use dispatch_once for read optimization, ensure that only the API is called once, and then simply access the variable directly.
weatherclient . h
#import "AFHTTPClient.h"
@interface Weatherclient:afhttpclient
+ (weatherclient *) sharedclient;
@end
weatherclient . M
#import "WeatherClient.h"
@implementation Weatherclient
+ (Weatherclient *) sharedclient
{
Static Weatherclient *sharedclient=nil;
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken,^{sharedclient =[[weatherclient alloc] Initwithbaseurl:[nsurl URLWithString:API]];
});
return sharedclient;
}
@end
in Software engineering, a singleton is a mathematical concept used to implement a single case, and the instantiation of a class is limited to the design pattern of only one object.
Or my understanding is:
Singleton is a class that can only instantiate an object.
Although this is a practical definition of a singleton, it is not necessarily the case in the foundation framework. Like whatNsfilemangerAndNsnotificationcenter,By their class method, respectively.DefaultmanagerAndDefaultcenterGet. Although not strictly a single case, these class methods return a shared instance of a class that can be accessed in all code in the app. We will also use this approach in this article. The best way to use objective-c to implement singleton patterns has been a lot of controversy, and developers (including Apple) seem to change their minds every few years. When Apple introduced Grand Central Dispatch (GCD) (Mac OS 10.6 and iOS4.0), they also introduced a function that was well suited for implementing singleton patterns. The function isdispatch_once:
void Dispatch_once (dispatch_once_t *predicate, dispatch_block_t block);
the function receives a predicate that dispatch_once is used to check whether the code block has been dispatched (it is a long integer and is actually used as a BOOL ). It also receives a block of code that wants to be dispatched only once during the lifetime of the application, for this example is used for instantiation of a shared instance. dispatch_once not only means that the code will only be run once, but also thread-safe, which means you don't need to use things like @synchronized to prevent the use of multiple threads or queues when they are not synchronized. Apple's GCD documentation confirms this:
if called by multiple threads, the function synchronizes and so on until the code block is complete.
How do you actually use these? Well, suppose there's aAccountmanagerclass, you want to access the shared instance of the class throughout your app. You can implement a class method simply by following this code:
+ (Accountmanager *) Sharedmanager {
static Accountmanager *sharedaccountmanagerinstance = nil;
static dispatch_once_t predicate;
Dispatch_once (&predicate, ^{
sharedaccountmanagerinstance = [[Self alloc] init];
});
return sharedaccountmanagerinstance;
}
This means that any time you access a shared instance, you need to do just the following:
Accountmanager *accountmanager = [Accountmanager Sharedmanager];
In this case, you now have a shared instance in your app that will only be created once. This approach has many advantages:
1 Thread safety 2 good to meet static analyzer requirements 3 and automatic reference count (ARC) compatible
4 requires only a small amount of codeof this methodDisadvantageis that it still runs to create a non-shared instance:
Accountmanager *accountmanager = [[Accountmanager alloc] init];
There are times when you want to have this behavior, but if you're looking for an instantiation of just one instance, you need to be aware of that.