Skip to content

Instantly share code, notes, and snippets.

@leviathan
Created November 6, 2010 16:40
Show Gist options
  • Save leviathan/665526 to your computer and use it in GitHub Desktop.
Save leviathan/665526 to your computer and use it in GitHub Desktop.
A category to send NSNotifications in the UI thread
// NSNotificationCenter+MainThread.h
@interface NSNotificationCenter (MainThread)
- (void)postNotificationOnMainThread:(NSNotification *)notification;
- (void)postNotificationOnMainThreadName:(NSString *)aName object:(id)anObject;
- (void)postNotificationOnMainThreadName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
@end
// NSNotificationCenter+MainThread.m
#import "NSNotificationCenter+MainThread.h"
@implementation NSNotificationCenter (MainThread)
- (void)postNotificationOnMainThread:(NSNotification *)notification {
[self performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];
}
- (void)postNotificationOnMainThreadName:(NSString *)aName object:(id)anObject {
NSNotification *notification = [NSNotification notificationWithName:aName object:anObject];
[self postNotificationOnMainThread:notification];
}
- (void)postNotificationOnMainThreadName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo {
NSNotification *notification = [NSNotification notificationWithName:aName object:anObject userInfo:aUserInfo];
[self postNotificationOnMainThread:notification];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment