10

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;
}
Community
  • 1
  • 1
cmac
  • 906
  • 10
  • 19
  • 1
    [This post will help you, please refer to it as to correctly implement your singleton][1] [1]: http://stackoverflow.com/questions/7598820/correct-singleton-pattern-objective-c-ios – chrislhardin Oct 27 '12 at 21:25
  • Why do you destroy the singleton and not just the buffer containing the data? – Jano Oct 27 '12 at 21:33
  • @Jano Could you explain how I might destroy that buffer? – cmac Oct 27 '12 at 21:38
  • I should add too: I wasn't worried about this until xCode crashed after testing the app a few times in the Simulator. Maybe that was unrelated, but I assumed I had created some kind of memory leak. – cmac Oct 27 '12 at 21:46
  • @cmac Releasing its contents. But I don't even know why you have a buffer. You could create NSManagedObjects on a NSManagedObjectContext directly and save or rollback when you are done. I would use the singleton just for core data related code, a NSManagedObjectContext is already a buffer. – Jano Oct 28 '12 at 12:07

1 Answers1

20

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Oops, of course (re: dispatch_once). Forgive me, nulling the instance variables is beneficial primarily because it will free up memory? – cmac Oct 27 '12 at 21:41
  • Yes, also it resets the state since you've saved the data you were working on. – jrturton Oct 28 '12 at 07:06
  • @jrturton I can not tell whether your answer says it is possible or not to destroy a singleton object. You say "You can't destroy this singleton" and then go ahead to say "You don't need to destroy the singleton". My experience with them is that it Is possible to destroy them, just unusual to have such a need, and as you mentioned- you can not recreate it if it was initially created within a dispatch_once(). Am just trying to get a straight answer, am not challenging you :) – pnizzle Jan 22 '15 at 00:13
  • @jrturton yes that's much better, thanks. I just saw the Macbook Axe on your twitter, love it! – pnizzle Jan 22 '15 at 02:11