CLLocationManager class from the CLLocationManager framework provides a single entry point to all GPS module services. Someone might think: if you can make a copy of CLLocationManager , is it possible to get an additional set of GPS services for your application? It sounds like a fantasy - you created two software GPS for the price of one hardware. But in reality, you still get only one GPS at a time, since the iPhone has only one GPS, which creates real connections with satellites. So, if you think you have created a super application that can manipulate two separate GPS connections at the same time, and want to brag about it to your friends, think twice.
uniqueInstance is a single instance of the class Singleton , represented as a class variable that the static method sharedInstance returns to clients. Normally sharedInstance will check if uniqueInstance is uniqueInstance . If not, the method will create it before returning. class Singleton { public: static Singleton *Instance(); protected: Singleton(); private: static Singleton *_instance; }; Singleton *Singleton::_instance = 0; Singleton *Singleton::Instance() { if (_instance == 0) { _instance = new Singleton; } return _instance; } Instance() static variable _instance checked to 0 ( NULL ). If so, a new object of class Singleton and then returned. Some of you might think that the Objective-C version is not much different from its counterpart and should look like in listings 7-2 and 7-3. @interface Singleton : NSObject { } + (Singleton *) sharedInstance; @end @implementation Singleton static Singleton *sharedSingleton_ = nil; + (Singleton *) sharedInstance { if (sharedSingleton_ == nil) { sharedSingleton_ = [[Singleton alloc] init]; } return sharedSingleton_; } @end #import "Singleton.h" @implementation Singleton static Singleton * sharedSingleton_ = nil; + (Singleton*) sharedInstance { if (sharedSingleton_ == nil) { sharedSingleton_ = [[super allocWithZone:NULL] init]; } return sharedSingleton_; } sharedInstance method, just as in the first example, it is first checked that a single instance of the class is created, otherwise a new one is created and returned. But this time, it calls [[super allocWithZone:NULL] init] to create a new instance instead of using other methods, such as alloc . Why super and not self ? This is because the basic methods for allocating memory for an object in our class are redefined, so you need to “borrow” this functionality from the base class, in this case from NSObject , to help make the low-level routine work of allocating memory for us. + (id) allocWithZone:(NSZone *)zone { return [[self sharedInstance] retain]; } - (id) copyWithZone:(NSZone*)zone { return self; } Singleton class that you need to think about. In the allocWithZone:(NSZone *)zone method, the class instance that is returned from the sharedInstance method sharedInstance . In the Cocoa Touch framework, when calling the class allocWithZone:(NSZone *)zone class method, the memory for the instance will be allocated, its reference count will be set to 1, and the instance will be returned. We have seen that the alloc method alloc used in many situations; in fact, alloc allocWithZone: with the zone set to NULL to allocate memory for the instance in the default zone. The details of creating an object and managing memory are outside of this book. You can consult the documentation for further clarification.copyWithZone:(NSZone*)zone method to make sure that it does not return a copy of the instance, but returns the same one, returning self . - (id) retain { return self; } - (NSUInteger) retainCount { return NSUIntegerMax; // , } - (void) release { // } - (id) autorelease { return self; } @end retain , release and autorelease , are redefined to make sure that they do nothing (in the memory management model with reference counting), except to return self . The retainCount method returns NSUIntegerMax (4,294,967,295) to prevent an instance from being deleted from memory during the life of the application.retain a Singleton object that is returned from the sharedInstance method in allocWithZone: but retain overridden and is actually ignored in our implementation. Given this, we will have the opportunity to make the Singleton class less “strict” (that is, it will be possible to allocate memory for additional instances and initialize them, but the factory sharedInstance method always returns the same instance or Singleton object becoming destructible). Subclasses can override the retain , release and autorelease again to provide a suitable implementation of memory management.Singleton ? Consider how to do this.alloc call alloc redirected to super , so the NSObject class NSObject take care of allocating memory for the object. If we inherit from the class Singleton without modifications, then the returned instance will always be of type Singleton . Since the Singleton class overrides all instance-related methods, it is rather difficult to inherit from it. But we were lucky; You can use some Foundation functions to instantiate any object based on its class type. One of them is id NSAllocateObject (Class aClass, NSUInteger extraBytes, NSZone *zone) . Therefore, if we want to instantiate an object of the class called “Singleton”, we can do the following: Singleton *singleton = [NSAllocateObject ([Singleton class], 0, NULL) init]; Singleton . The second parameter is intended for any number of additional bytes for indexed instance variables, which is always 0. The third parameter is the designation of the zone of allocated memory; the default zone is almost always used (the parameter is NULL ). Therefore, you can instantiate any objects using this function, knowing the class type. What should I do to inherit from the class Singleton ? Let's remember that the original version of the sharedInstance method looks like this: + (Singleton*) sharedInstance { if (sharedSingleton_ == nil) { sharedSingleton_ = [[super allocWithZone:NULL] init]; } return sharedSingleton_; } NSAllocateObject to create an instance, it will become like this: + (Singleton *) sharedInstance { if (sharedSingleton_ == nil) { sharedSingleton_ = [NSAllocateObject([self class], 0, NULL) init]; } return sharedSingleton_; } Singleton or one of its subclasses, this version will do everything correctly.Singleton in the example is only good for general use. If you need to use a singleton object in a multi-threaded environment, then you need to make it thread-safe. To do this, you can insert @synchronized() blocks or use NSLock instances around checking for nil for the sharedSingleton_ static variable variable. If there are any other properties that need to be protected too, then you can make them atomic .UIApplication , UIAccelerometer and NSFileManager .UIApplication class. It provides a centralized point of control and coordination for iOS applications.UIApplication . It is created as a singleton object by the UIApplicationMain function when the application is started and is available at runtime via the sharedApplication class sharedApplication .UIApplication object performs many service tasks for the program, including initial routing of incoming user messages, as well as dispatching action messages for UIControl objects to the corresponding target objects. It maintains a list of all open UIWindow objects. The application object is associated with the UIApplicationDelegate application delegate object, which is informed of any significant events during the execution of the program, such as startup, low memory warnings, application termination, and background processes. The event handlers allow the delegate to customize the behavior of the application.UIAccelerometer . The UIAccelerometer class allows an application to subscribe to receive acceleration-related data from the onboard accelerometer in an iOS device. An application can use data on changes in linear acceleration along the main axes in three-dimensional space to determine both the current orientation of the device and instantaneous changes in orientation.UIAccelerometer class is a singleton, so you cannot create its objects explicitly. To access its single instance, you need to call the class method sharedAccelerometer . In addition, you can set its updateInterval property and delegate property to your own delegate object to receive any reported acceleration data from a singleton instance.NSFileManager class was once a “strict” implementation of the Singleton pattern in front of Mac OS X 10.5 and in iOS 2.0. Calling his init method does nothing, and its only instance can be created and accessed only through the defaultManager class defaultManager . However, since the singleton implementation is not thread-safe, new NSFileManager instances must be created to ensure this security. This approach is viewed as a more flexible implementation of the Singleton, in which the factory method always returns the same instance, but additional instances can also be selected and initialized.allocWithZone: and other related methods.Source: https://habr.com/ru/post/198470/
All Articles