• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: iOS: Preventing a singleton from being deallocated when the app is in the background.


  • Subject: Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
  • From: Steve Christensen <email@hidden>
  • Date: Wed, 19 Oct 2016 12:08:23 -0700

This is the model I use for singletons and I've never had the singleton instance deallocated out from under me.

+ (MyClass*) sharedThing
{
	static dispatch_once_t sOnceToken = 0;
	static MyClass* sSharedThing = nil;

	dispatch_once(&sOnceToken,
	^{
		sSharedThing = [[MyClass alloc] init];
	});

	return sSharedThing;
}



> On Oct 19, 2016, at 11:41 AM, Alex Zavatone <email@hidden> wrote:
>
> We are running into what appears to be a case where a singleton that has been created through GCD dispatch_once may be getting deallocated by the OS.
>
> To check and see if this is the case, I have put logging code in the dealloc method, but our logs do show the init method being called twice even though the method is past the dispatch_once.
>
> I'm interested in seeing if I can prevent the singleton from being deallocated.
>
> I have seen on discussion where people suggest keeping a private strong property in the singleton with a reference to self to prevent this from happening.
>
> Something like
>    self.selfReference = self;
>
> within an interface extension.
>
> Naturally, this is raising some eyebrows with other members of the team, but currently, no one has a solution to make sure that this core singleton doesn't get deallocated when the app is in the background and has been running for a while.
>
> Does this sound sane or a horrible idea?
>
> If it does sound like a horrible idea, how would you suggest preventing an important singleton from being deallocated?
>
> Thanks in advance,
> Alex Zavatone


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
      • From: Alex Zavatone <email@hidden>
References: 
 >iOS: Preventing a singleton from being deallocated when the app is in the background. (From: Alex Zavatone <email@hidden>)

  • Prev by Date: iOS: Preventing a singleton from being deallocated when the app is in the background.
  • Next by Date: Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
  • Previous by thread: iOS: Preventing a singleton from being deallocated when the app is in the background.
  • Next by thread: Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
  • Index(es):
    • Date
    • Thread