Skip to content

Commit

Permalink
Simplified the NSNotificationCenterThreadingAdditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeydom committed Aug 10, 2018
1 parent a2e9f11 commit 33eda53
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions framework/NSNotificationCenterThreadingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,18 @@

@implementation NSNotificationCenter (NSNotificationCenterThreadingAdditions)

+ (void)_postNotification:(NSNotification *)aNotification {
[[self defaultCenter] postNotification:aNotification];
}

+ (void)_postNotificationViaDictionary:(NSDictionary *)anInfoDictionary {
NSString *name = [anInfoDictionary objectForKey:@"name"];
id object = [anInfoDictionary objectForKey:@"object"];
[[self defaultCenter] postNotificationName:name
object:object
userInfo:nil];
- (void)postNotificationOnMainThread:(NSNotification *)notification {
if (pthread_main_np()) {
return [self postNotification:notification];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self postNotification:notification];
});
}
}


- (void)postNotificationOnMainThread:(NSNotification *)aNotification {
if( pthread_main_np() ) return [self postNotification:aNotification];
[[self class] performSelectorOnMainThread:@selector( _postNotification: ) withObject:aNotification waitUntilDone:NO];
- (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object {
[self postNotificationOnMainThread:[NSNotification notificationWithName:name object:object]];
}

- (void) postNotificationOnMainThreadWithName:(NSString *)aName object:(id)anObject {
if( pthread_main_np() ) return [self postNotificationName:aName object:anObject userInfo:nil];
NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:2];
if (aName) {
[info setObject:aName forKey:@"name"];
}
if (anObject) {
[info setObject:anObject forKey:@"object"];
}
[[self class] performSelectorOnMainThread:@selector(_postNotificationViaDictionary:)
withObject:info
waitUntilDone:NO];
}
@end

0 comments on commit 33eda53

Please sign in to comment.