BaseModel icon indicating copy to clipboard operation
BaseModel copied to clipboard

hasSharedInstance issue

Open mrtristan opened this issue 11 years ago • 2 comments

so perhaps i'm not properly nullifying my shared instances... i'm doing so by calling setSharedInstance:nil on a class. is this correct?

my issue here is after doing the above, hasSharedInstance returns true.

I drilled down in to the code and after expanding

+ (BOOL)hasSharedInstance
{
    return objc_getAssociatedObject(self, BMSharedInstanceKey) != nil;
}

to

+ (BOOL)hasSharedInstance
{
    id obj = objc_getAssociatedObject(self, BMSharedInstanceKey);
    return obj != nil;
}

before that return statement, that obj is set to an instance of the class it is called upon with all properties of the class as nil which of course means that that evaluation at the end yields true. is this by design? my expectation was that it would return false.

mrtristan avatar Apr 08 '14 16:04 mrtristan

I don't really understand what you mean by this. objc_getAssociatedObject(self, BMSharedInstanceKey) will return nil if the value associated to the BMSharedInstanceKey is nil, which it should be if you setSharedInstance:nil.

nicklockwood avatar Aug 24 '14 13:08 nicklockwood

sorry, never saw the response on this.

i think this might be a clearer way to explain, here is the flow of the issue:

[MyClass setSharedInstance:nil];
[MyClass hasSharedInstance]; //evaluates to true

when debugging the above example, i expanded your method as mentioned in the original post and observed id to not actually be nil, but as being an instantiated instance of the object with all properties set to nil.

does that help or make the issue more clear?

mrtristan avatar Feb 27 '15 19:02 mrtristan