Re: iOS: Preventing a singleton from being deallocated when the app is in the background.
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: Alex Zavatone <email@hidden>
- Date: Wed, 19 Oct 2016 14:54:30 -0500
On Oct 19, 2016, at 2:08 PM, Steve Christensen wrote:
> 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;
> }
>
It appears that this is what I am doing. Though the details are not the same, should this matter?
We have already had to patch one instance where the ref to this was getting lost
+ (instancetype)referenceGeofenceController {
static GeofenceControllerSingleton *geofenceController;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
geofenceController = [[self alloc] init];
});
return geofenceController;
}
Is there anything wrong with what I'm doing? should I be using [GeofenceControllerSingleton alloc] init] instead of [[self alloc] init]?
Thanks a million.
Alex Zavatone
>
>
>> 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