Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How can I destroy a Singleton in ARC? Should I?

I have a singleton class that accumulates data until that data is written to my database (if you want to know why I'm implementing things this way, see here). After saving the data, I would like to destroy the singleton. How can I do this in ARC? Or am I being paranoid and do I need to destroy it at all?

*You might say that this is a duplicate of this question, but the accepted answer here is not specific enough to be helpful. It says "You can declare a method/function you call explicitly." What might the code for this look like? If I can't release the object outside a method, how can I possibly pull it off inside a method? It also says "The simplest way is to have a static C++ class hold it, then release it in its destructor." I don't know C++, but - can you really put a C++ class in your app code?

My singleton is implemented like so:

+(NHCFamilyStatus *)familyStatus
{
  static dispatch_once_t pred;
  static NHCFamilyStatus *familyStatusSharedObject=nil;

    dispatch_once(&pred, ^
    {
        familyStatusSharedObject = [[NHCFamilyStatus alloc] init];
    });

  return familyStatusSharedObject;
}
like image 695
cmac Avatar asked Oct 27 '12 21:10

cmac


1 Answers

If you destroy this singleton, you'll never be able to create it again (that's what the dispatch_once call means).

You don't need to destroy the singleton. By all means have a method on the singleton that removes any instance variables you no longer need, but there is no need to do anything else.

like image 74
jrturton Avatar answered Oct 25 '22 22:10

jrturton