Skip to content

Instantly share code, notes, and snippets.

@axiixc
Created May 23, 2011 22:04
Show Gist options
  • Save axiixc/987725 to your computer and use it in GitHub Desktop.
Save axiixc/987725 to your computer and use it in GitHub Desktop.
So fun story, notification observers are invoked on the thread from which you send the notification. Also I like GCD.
#import <dispatch/dispatch.h>
@interface NSNotificationCenter (Concurrency)
+ (void)onMainQueuePostNotificationName:(NSString *)notificationName object:(id)object;
+ (void)onMainQueuePostNotificationName:(NSString *)notificationName object:(id)object userInfo:(NSDictionary *)userInfo;
+ (void)onQueue:(dispatch_queue_t)queue postNotificationName:(NSString *)notificationName object:(id)object;
+ (void)onQueue:(dispatch_queue_t)queue postNotificationName:(NSString *)notificationName object:(id)object userInfo:(NSDictionary *)userInfo;
@end
#import "NSNotificationCenter+Concurrency.h"
@implementation NSNotificationCenter (Concurrency)
+ (void)onMainQueuePostNotificationName:(NSString *)notificationName object:(id)object
{
[self onQueue:dispatch_get_main_queue() postNotificationName:notificationName object:object];
}
+ (void)onMainQueuePostNotificationName:(NSString *)notificationName object:(id)object userInfo:(NSDictionary *)userInfo
{
[self onQueue:dispatch_get_main_queue() postNotificationName:notificationName object:object userInfo:userInfo];
}
+ (void)onQueue:(dispatch_queue_t)queue postNotificationName:(NSString *)notificationName object:(id)object
{
dispatch_async(queue, ^{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:object];
});
}
+ (void)onQueue:(dispatch_queue_t)queue postNotificationName:(NSString *)notificationName object:(id)object userInfo:(NSDictionary *)userInfo
{
dispatch_async(queue, ^{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:object userInfo:userInfo];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment