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

1

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;
}
Messaline answered 27/10, 2012 at 21:17 Comment(5)
[This post will help you, please refer to it as to correctly implement your singleton][1] [1]: #7599320Pixie
Why do you destroy the singleton and not just the buffer containing the data?Lepidopteran
@Lepidopteran Could you explain how I might destroy that buffer?Messaline
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.Messaline
@Messaline 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.Lepidopteran
P
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.

Presbyterial answered 27/10, 2012 at 21:28 Comment(4)
Oops, of course (re: dispatch_once). Forgive me, nulling the instance variables is beneficial primarily because it will free up memory?Messaline
Yes, also it resets the state since you've saved the data you were working on.Presbyterial
@Presbyterial 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 :)Caius
@Presbyterial yes that's much better, thanks. I just saw the Macbook Axe on your twitter, love it!Caius

© 2022 - 2024 — McMap. All rights reserved.