source: branches/gsoc08-framework/MacPorts_Framework_Release/Notification+ThreadAdditions.m @ 39738

Last change on this file since 39738 was 39738, checked in by armahg@…, 9 years ago

Adding Notification+ThreadAdditions categories to enable posting NSNotifications to main thread. Thanks to TH from CocoaDev for sample code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1//
2//  Notification+ThreadAdditions.m
3//  MacPorts.Framework
4//
5//  Created by George  Armah on 9/1/08.
6//  Copyright 2008 Lafayette College. All rights reserved.
7//
8
9#import "Notification+ThreadAdditions.h"
10#import <pthread.h>
11
12
13@implementation NSNotificationCenter (NSNotificationCenterAdditions)
14- (void) postNotificationOnMainThread:(NSNotification *) notification {
15        if( pthread_main_np() ) return [self postNotification:notification];
16        [self postNotificationOnMainThread:notification waitUntilDone:NO];
17}
18
19- (void) postNotificationOnMainThread:(NSNotification *) notification waitUntilDone:(BOOL) wait {
20        if( pthread_main_np() ) return [self postNotification:notification];
21        [[self class] performSelectorOnMainThread:@selector( _postNotification: ) withObject:notification waitUntilDone:wait];
22}
23
24+ (void) _postNotification:(NSNotification *) notification {
25        [[self defaultCenter] postNotification:notification];
26}
27
28- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object {
29        if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:nil];
30        [self postNotificationOnMainThreadWithName:name object:object userInfo:nil waitUntilDone:NO];
31}
32
33- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object userInfo:(NSDictionary *) userInfo {
34        if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:userInfo];
35        [self postNotificationOnMainThreadWithName:name object:object userInfo:nil waitUntilDone:NO];
36}
37
38- (void) postNotificationOnMainThreadWithName:(NSString *) name object:(id) object userInfo:(NSDictionary *) userInfo waitUntilDone:(BOOL) wait {
39        if( pthread_main_np() ) return [self postNotificationName:name object:object userInfo:userInfo];
40       
41        NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] init];
42        [info setObject:name forKey:@"name"];
43        if( object ) [info setObject:object forKey:@"object"];
44        if( userInfo ) [info setObject:userInfo forKey:@"userInfo"];
45       
46        [[self class] performSelectorOnMainThread:@selector( _postNotificationName: ) withObject:info waitUntilDone:wait];
47        [info release];
48}
49
50+ (void) _postNotificationName:(NSDictionary *) info {
51        NSString *name = [info objectForKey:@"name"];
52        id object = [info objectForKey:@"object"];
53        NSDictionary *userInfo = [info objectForKey:@"userInfo"];
54       
55        [[self defaultCenter] postNotificationName:name object:object userInfo:userInfo];
56}
57@end
58
59@implementation NSNotificationQueue (NSNotificationQueueAdditions)
60- (void) enqueueNotificationOnMainThread:(NSNotification *) notification postingStyle:(NSPostingStyle) postingStyle {
61        if( pthread_main_np() ) return [self enqueueNotification:notification postingStyle:postingStyle coalesceMask:( NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender ) forModes:nil];
62        [self enqueueNotificationOnMainThread:notification postingStyle:postingStyle coalesceMask:( NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender ) forModes:nil];
63}
64
65- (void) enqueueNotificationOnMainThread:(NSNotification *) notification postingStyle:(NSPostingStyle) postingStyle coalesceMask:(unsigned) coalesceMask forModes:(NSArray *) modes {
66        if( pthread_main_np() ) return [self enqueueNotification:notification postingStyle:postingStyle coalesceMask:coalesceMask forModes:modes];
67       
68        NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] init];
69        [info setObject:notification forKey:@"notification"];
70        [info setObject:[NSNumber numberWithUnsignedInt:postingStyle] forKey:@"postingStyle"];
71        [info setObject:[NSNumber numberWithUnsignedInt:coalesceMask] forKey:@"coalesceMask"];
72        if( modes ) [info setObject:modes forKey:@"modes"];
73       
74        [[self class] performSelectorOnMainThread:@selector( _enqueueNotification: ) withObject:info waitUntilDone:NO];
75        [info release];
76}
77
78+ (void) _enqueueNotification:(NSDictionary *) info {
79        NSNotification *notification = [info objectForKey:@"notification"];
80        NSPostingStyle postingStyle = [[info objectForKey:@"postingStyle"] unsignedIntValue];
81        unsigned coalesceMask = [[info objectForKey:@"coalesceMask"] unsignedIntValue];
82        NSArray *modes = [info objectForKey:@"modes"];
83       
84        [[self defaultQueue] enqueueNotification:notification postingStyle:postingStyle coalesceMask:coalesceMask forModes:modes];
85}
86@end
Note: See TracBrowser for help on using the repository browser.