How do I ensure there is only one instance of a class at any one time in Objective-C?

送分小仙女□ 提交于 2019-12-13 02:33:17

问题


This is slightly different from the standard singleton pattern, in that if all external references to an object have released, then the singleton will be released, too. Then, later, when a new object is requested, a new singleton is created. So, something like this:

MyThing *thing1 = [MyThing new];
MyThing *thing2 = [MyThing new];
// thing1 & thing2 are the same object.

thing1 = nil;
thing2 = nil;

thing1 = [MyThing new];
thing2 = [MyThing new];
// thing1 and thing2 are a the same objet, but it's a different object than above.

I tried to use a weak static variable to hang on to my scoped singleton, but that didn't work, since I have no way to increment the retain count under ARC. Which leaves me wondering: is this even possible?


回答1:


override allocWithZone: to manage a single static instance. If nil, makes a new one, if not nil, returns a retained version of it.

implement dealloc and when called nil the single static instance.

I'm not certain if this will work in ARC, you may need to disable arc for that file.

How expensive is it to keep the object around? It's certainly less hassle to follow a standard singleton pattern and just forget about it.




回答2:


Based on @KirkSpaziani's answer, here's what I've come up with:

static __weak MyTHing *currentThing = nil;

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    __block id thing = nil;
    dispatch_sync(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if (currentThing == nil) {
            currentThing = thing = [super allocWithZone:zone];
        } else {
            thing = currentThing;
        }
    });
    return thing;
}

- (void)dealloc {
    dispatch_sync(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        currentThing = nil;
    });
}

This assumes that the initializer can deal with "dirty data", as described by Mike Ash.



来源:https://stackoverflow.com/questions/26683532/how-do-i-ensure-there-is-only-one-instance-of-a-class-at-any-one-time-in-objecti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!