简体   繁体   English

在Objective-C(ios)中,有人使用过单例实例吗?

[英]In objective-C (ios) ,Has anybody used singleton instance?

从单例实例的内存角度来看,我们如何删除创建的单例实例,因为它已被放入自动释放池中?

Singleton instance shouldn't be placed in autorelease pool. 不应将Singleton实例放置在自动释放池中。 Singleton instance should be created once (usually when first referenced) and deleted when the application terminates (I mean automatically by iOS). Singleton实例应该创建一次(通常是在首次引用时),并在应用程序终止时删除(我的意思是iOS会自动删除)。 This is why singleton is usually assigned to a static variable. 这就是为什么通常将单例分配给静态变量的原因。

You should increase the reference counter (retain) the singleton instance when assigning to that static variable. 分配给该静态变量时,应增加引用计数器(保留)单例实例。 At that point even if you add it to an autorelease pool it won't be deleted as it is already retained somewhere else. 此时,即使将其添加到自动释放池中也不会被删除,因为它已经保留在其他位置。

To delete that singleton instance you would simply need to release the current object assigned to the static variable (eg release) and assign nil or create a new singleton. 要删除该单例实例,您只需要释放分配给静态变量的当前对象(例如release),然后分配nil或创建一个新的单例。 If the same instance has been added to an autorelease pool it won't be deleted immediately, only after that autorelease pool has been deleted itself. 如果将同一实例添加到自动释放池中,则不会立即删除该实例,只有在该自动释放池本身已删除后才可以将其删除。 But it shouldn't change much in your application as the singleton is already nil or recreated as a new instance, thus any further calls will retrieve the new instance. 但它在您的应用程序中应该不会有太大变化,因为单例已经为零或已重新创建为新实例,因此任何进一步的调用都将检索新实例。

Again, I don't see any reason why you would add a singleton to autorelease pool. 再说一次,我看不出为什么要在自动释放池中添加单例。 Please share a snippet of code if this doesn't answer your question. 如果无法回答您的问题,请分享一段代码。

There's some debate on how to create a singleton. 关于如何创建单例,存在一些争论。 I use the following pattern: 我使用以下模式:

+ (MYSingletonClass *) sharedInstance
{
    static dispatch_once_t onceToken;
    static MYSingletonClass * __sharedInstance = nil;

    dispatch_once(&onceToken, ^{
        __sharedInstance = [[self alloc] init];
    });

    return __sharedInstance;
}

On clarification by the OP, it turns out this doesn't answer the question, but I thought I'd share anyway :) 经OP澄清,事实证明这并不能解决问题,但我认为我还是会分享的。

See the this post comparing @synchronized v dispatch_once 看到这篇文章比较@synchronized v dispatch_once

As far as I know, Singletons are helpful because you do NOT release them until your app is closed. 据我所知,单身是有帮助的,因为你,直到你的应用程序被关闭释放他们。 So your data are always available. 因此,您的数据始终可用。

If you need to free memory I suggest you find a different way to menage data... 如果您需要释放内存,建议您使用另一种方式管理数据...

static id sharedInstance=nil;

+(id)sharedInstance 
{
    @synchronized(self) 
    {
        if(!sharedInstance)
       {
           NSLog(@"Allocated");
           sharedInstance = [[self alloc] init];
       }
   }
   return sharedInstance;
}
//standard way to declare singleton object

如果您需要清除其内容,也许可以向您的单例对象添加“清除”方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM