Thursday, November 8, 2012

dispatch_once

dispatch_once is synchronous and its saying "perform something once and only once".It replaces the following:

+ (MyClass *)sharedInstance {
    static MyClass *sharedInstance;
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[MyClass alloc] init];
        }
    }
    return sharedInstance;
}
Advantage of dispatch_once is its faster.

No comments:

Post a Comment