带ARC的单身人士

use*_*028 14 singleton objective-c automatic-ref-counting

我的问题如下:我有一个单例类型的对象(我正在使用ARC),在实现文件中有这个代码

+(id)sharedInstance 
{
    static DataManager *sharedInstance;
    if (sharedInstance == nil) {
        sharedInstance = [[DataManager alloc] init];
    }
    return sharedInstance;
}

+(NSManagedObjectContext *)getManagedContext
{
    AppDelegate *applicationDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
    return [applicationDelegate managedObjectContext];
}

+(void)saveContext:(NSManagedObjectContext *)context
{
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
}

#pragma mark - Data management methods

+(void)addPersonWithName:(NSString *)name andPicture:(UIImage *)picture
{
    NSManagedObjectContext *context = [self getManagedContext]; //no problem here
    //some code 
    [self saveContex:context]; // no known class method for selector saveContext:
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?该方法在.h文件中声明为+ ... getManagedContext模型没有给出这个错误????

Jan*_*ano 61

方法中的关键字self引用方法的所有者,该方法是实例对象的实例,以及类方法的类.但是,saveContex结尾处缺少消息(saveContext).

dispatch_once singleton

这是一个与ARC兼容的更好的单身成语:

+(MySingleton *)sharedInstance {
    static dispatch_once_t pred;
    static MySingleton *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[MySingleton alloc] init];
    });
    return shared;
}
Run Code Online (Sandbox Code Playgroud)

与Xcode模板相同的代码

与带有占位符的Xcode模板相同的代码:

+ (<#class#> *)shared<#name#> {
    static dispatch_once_t onceToken;
    static <#class#> *shared<#name#> = nil;
    dispatch_once(&onceToken, ^{
        shared<#name#> = <#initializer#>;
    });
    return shared<#name#>;
}
Run Code Online (Sandbox Code Playgroud)

相同的代码+禁用alloc/init/new

想告诉用户他们应该调用sharedInstancealloc/init/new吗?您可以禁用属性不可用的方法.如果在类上调用任何这些方法,这将导致编译器错误.

#import <Foundation/Foundation.h>

@interface MySingleton : NSObject

+(instancetype) sharedInstance;

// clue for improper use (produces compile time error)
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));

@end

#import "MySingleton.h"

@implementation MySingleton

+(instancetype) sharedInstance {
    static dispatch_once_t pred;
    static id shared = nil;
    dispatch_once(&pred, ^{
        shared = [[super alloc] initUniqueInstance];
    });
    return shared;
}

-(instancetype) initUniqueInstance {
    return [super init];
}

@end
Run Code Online (Sandbox Code Playgroud)

警告:dispatch_once不可重入

不要sharedInstancedispatch_once块内部进行递归调用.

如果dispatch_once从多个线程调用,它将成为阻止并发访问的障碍.但是如果你在块内部的同一个线程中再次调用它,它将使线程死锁.这个例子说明了这个问题:

#import <Foundation/Foundation.h>

static NSRecursiveLock *_lock = nil;
// constructor = run before main. used = emit code even if the function is not referenced.
// See http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
static void runBeforeMain(void) __attribute__ ((constructor, used));
static void runBeforeMain(void) {
    _lock = [NSRecursiveLock new];
}

static void test(void) 
{
    static NSUInteger count = 0;
    NSLog(@"iteration #%lu", ++count);

    // WRONG: deadlock!
    //static dispatch_once_t token;
    //dispatch_once(&token, ^{
    //  test();
    //});

    // OK
    [_lock lock];
    test();
    [_lock unlock];

    --count;
}

int main(int argc, char **argv) {
    @autoreleasepool {
        test();
    }
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

+初始化单身

使用+ initialize是创建单例的另一种习惯用法.优点:比它快几倍dispatch_once.缺点:+initialize每个类调用一次,因此如果您对单例进行子类化,则还将为每个父类创建一个实例.仅当您知道单例不会被子类化时才使用它.

static id sharedInstance;

+ (void) initialize {
    // subclassing would result in an instance per class, probably not what we want
    NSAssert([MySingleton class] == self, @"Subclassing is not welcome");
    sharedInstance = [[super alloc] initUniqueInstance];
}

+(instancetype) sharedInstance {
    return sharedInstance;
}
Run Code Online (Sandbox Code Playgroud)