如何适当地管理单例类的内存?

时间:2015-11-11 09:02:57

标签: ios memory-management background singleton nsobject

我有一个NSObject子类,如下所示:

@interface MyManager : NSObject

+ (MyManager *)sharedInstance;

// More instance methods

@end

@implementation MyManager

+ (MyManager *)sharedInstance
{
   static dispatch_once_t _singletonPredicate;
   static MyManager *sharedObject = nil;

   dispatch_once(&_singletonPredicate, ^{
       sharedObject = [[self alloc] init];
   });

   return sharedObject;
}

// More instance methods implemetation

@end

我用这种方式在整个应用程序中调用它:

[[MyManager sharedInstance] anInstanceMethod];

无需将其链接到任何地方的任何财产。这是正确的还是适当的,还是应该建议在任何地方保留这类物品的财产?

另一方面,我启用了应用程序以保持在后台模式下更新位置,并且我需要此对象在应用程序在后台运行时继续侦听位置管理器中的事件。在这种情况下我该如何处理这个对象?

提前致谢

1 个答案:

答案 0 :(得分:0)

Singleton一旦创建就存在,永远不会破坏。