In the sharedMySingleton method there is no need to call persistence?
alloc
returns a new instance with reference counting, so saving is not required.
If not, why is there a persistence in allocWithZone?
According to the rules, when you call allocWithZone
, you own the link, so allocWithZone
significantly increase the reference count for you. But this implementation of allocWithZone returns an instance of a singleton that has already been created and belongs to someone else (the sharedMySingleton
method). Thus, the sharedMySingleton
method creates an object with alloc
, so it becomes the owner. And then you get the same instance through allocWithZone
, so you become the second owner of the same instance. Thus, the conservation account should increase, because now there are two owners. This is why allocWithZone
needs to be saved.
What is @synchronized?
@synchronized
allows code to be invoked simultaneously by multiple threads. If you never call sharedMySingleton
from more than one thread, then this is not necessary, and you can omit it.
NSAssert believes that a block can be called many times, so if so, there should be one more code to release the previous memory, or it can be clearly seen that without NSAsserting, and if not, then why is there this NSAssert?
Since the class is designed for singleton, alloc
should only be called once. NSAssert()
terminates the program if alloc
is called more than once. Because NSAssert()
terminates the program when alloc
is called a second time, memory management is not required.
source share