Snowman
Snowman

Reputation: 32061

Static variables and multiple threads in Objective-C

I'm writing a category for NSNotificationCenter with a class method:

+(void)postNotificationName:(NSString*)name onMainThread:(BOOL)mainThread withObject:(id)object;

I'm doing this to make sure that I'm explicit with which thread my notifications are delivered, as this caused me a lot of hidden problems. And so just to avoid doing an extra look up every time I post a notification by calling [NSNotificationCenter defaultCenter] every time, I thought I must just create a static variable once:

static NSNotificationCenter *defaultCenter;
+(void)postNotificationName:(NSString*)name onMainThread:(BOOL)mainThread withObject:(id)object
{
    if(!defaultCenter)
        defaultCenter = [NSNotificationCenter defaultCenter];
    if(mainThread) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [defaultCenter postNotificationName:name object:object];
        });
    } else {
        [defaultCenter postNotificationName:name object:object];
    }
}

Is what I'm doing safe? What if defaultCenter is initially defined on a background thread and then used again on the main thread - is this asking for trouble? Is there a better way to do this?

Upvotes: 1

Views: 1377

Answers (2)

Jody Hagins
Jody Hagins

Reputation: 28349

I agree with Joe in regard to the static. In addition, I'd be very surprised if you were even able to measure the difference in your app between calling defaultCenter and using a cached value.

It's probably already implemented with something like dispatch_once, which is quite fast.

Furthermore, may I suggest using something that already exists to make sure you receive notifications on the proper thread?

How about an existing method,

- (id)addObserverForName:(NSString *)name
                  object:(id)obj
                   queue:(NSOperationQueue *)queue
              usingBlock:(void (^)(NSNotification *))block

It has several benefits, first that it is a block-based API, which is wonderful for handling notifications. Your handling code is right where it should be.

Anyway, notice the third parameter. You can give it an operation queue, and the notification will be received on that queue, irregardless of which thread it was sent from.

It is also a cleaner interface, because the receiver is specifying on which queue the notification is handled, decoupling the sender from that responsibility.

Upvotes: 2

Joe
Joe

Reputation: 57169

I would recommend not creating a static variable for this and just get the defaultCenter each time. Otherwise you will need to create proper locking mechanism which will likely duplicate any lock checks that may are already implemented in defaultCenter which could result in worse performance/thread safety. If you want to still go this route look at the question What does your Objective-C singleton look like?

Upvotes: 2

Related Questions