SoFunction
Updated on 2025-04-11

Example of Creation and Destruction of iOS Singleton

Singleton: Singleton pattern makes a class have only one instance. Singleton is in the process of using it to ensure that there is a unique instance globally. In this way, the functions of unified management can be met. For example, a database only requires unified reading and writing operations globally. Do not read and write multiple instances. d singleton is the only instance, which is not the same as the life cycle that always accompanys the app. Below, I will analyze singletons from the creation and destruction of singletons.

Creation of singletons

The creation of singletons is divided into arc and mrc, and the creation in two modes.

Creation under ARC

  • First define a static instance. static MyClass _instance;
  • Override the allocWithZone method. This method must call the method by allocating space to the object.
  • Define a share class method that can be called globally. This method needs to consider thread safety issues
  • If you need copy, you need to comply with the NSCopying protocol, and in copyWithZone, you need to return to self directly;

example

static Myclass _instance;

Method 1:

+(id)shareInstance{ 
   @synchronized(self){
    if(_instance == nil)
       _instance = [MyClass alloc] init]; 
   }
   return _instance;
}

Method 2:

+(id)shareInstance{
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
   if(_instance == nil)
       _instance = [MyClass alloc] init]; 
  });
   return _instance;
}

Both of the above methods are thread-safe. However, Apple now advocates method two.

This method exists for historical reasons; memory zones are no longer used by Objective-C. You should not override this method.

//Rewrite allocWithZone, and the implementation is consistent with method one and method two.+(id)allocWithZone:(struct _NSZone *)zone{
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
   if(_instance == nil)
      _instance = [MyClass alloc] init]; 
  });
   return _instance;
} 

This function is rewriting is wrong. Please pay attention to readers.

//Make sure the same when copying-(id)copyWithZone:(NSZone *)zone{ 
  return _instance; 
}

This is a complete singleton, which is guaranteed to be unique no matter how you create it.

Creation under MRC The creation process is the same as the steps under ARC. However, some memory management functions need to be processed.

//No counter +1 is required- (id)retain { 
  return self; 
} 

//Not required. Objects in the heap area are only required- (id)autorelease { 
  return self; 
} 

//unnecessary- (oneway void)release { 
} 

//No counters are required. Directly return the maximum unsigned integer- (NSUInteger)retainCount { 
  return UINT_MAX; //Refer to the retainCount of the constant area string}

This ensures that this singleton will not be released unintentionally.

Destruction of singletons

I mentioned the creation of singletons before, but there are some cases where singletons need to be destroyed.

The following are two ways of destruction corresponding to two forms of destruction from two creation methods.

Method 1:

+(void)attemptDealloc{
  [_instance release]; //mrc needs to be released, of course you cannot rewrite the release method.  _instance = nil;
}

Method 2:

1. You must take static dispatch_once_t onceToken; this must be taken out of the function and becomes global.

2.

+(void)attempDealloc{
  onceToken = 0; 
  // Only when set to 0 will GCD be considered to have never been executed. It defaults to 0. This ensures that the object is created again the next time shareInstance is called again.  [_instance release];
  _instance = nil;
 }

The above two methods are methods to destroy singletons.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.