IOS Singleton mode Dispatch_once
Some variables need to be initialized only once (such as reading configuration parameters from a file). Read device model, etc.), can use Dispatch_once to perform read optimization. Make sure you just call the API once, and then just go directly to the variables.
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 project, a singleton is a mathematical concept for implementing a singleton, limiting the instantiation of a class to the design pattern of only one object.
Or my understanding is:
a singleton is a class. This class can only instantiate an object.
Although this is a practical definition of a singleton, it is not necessarily the case in the foundation framework. ExampleNsfilemangerAndNsnotificationcenter. By their class method, respectively.DefaultmanagerAndDefaultcenterGet. While not strictly a single case, these class methods return a shared instance of the class that can be visited in all the code in the app. We will also use this method in this article. The best way to use objective-c to implement singleton patterns has always been a lot of controversy. 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 is well suited for implementing singleton patterns. The function is dispatch_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 (is a long integer. Actually used as BOOL ). It also receives a block of code that wants to be dispatched only once during the lifetime of the application. For this example, it is used for instantiation of a shared instance. dispatch_once not only means that the code will only be executed once and is thread-safe, which means that you do not 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:
assumed to be called by multiple threads. The function synchronizes and so on until the code block is complete.
How do you actually use these? Well, if there's aAccountmanagerclass, you want to access the shared instance of the class throughout your application. You can implement a class method simply by following the code, for example:
+ (Accountmanager *) Sharedmanager {
static Accountmanager *sharedaccountmanagerinstance = nil;
static dispatch_once_t predicate;
Dispatch_once (&predicate, ^{
sharedaccountmanagerinstance = [[Self alloc] init];
});
return sharedaccountmanagerinstance;
}
This means that whenever you visit 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 is only created once. There are many advantages to this approach:
1 Thread Safety 2 very good satisfied static analyzer requires 3 and self active Reference count (ARC) compatible
4 requires only a small amount of codeof this methodDisadvantageis that it still executes to create a non-shared instance:
Accountmanager *accountmanager = [[Accountmanager alloc] init];
There are times when you want to behave like this, but suppose you want to be aware that only one instance is instantiated.
IOS Singleton mode Dispatch_once