-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGYNotificationOberverIdentifer.m
164 lines (117 loc) · 4.36 KB
/
GYNotificationOberverIdentifer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// GYNotificationOberverIdentifer.m
// WeRead
//
// Created by 王斌 on 16/7/4.
// Copyright © 2016年 tencent. All rights reserved.
//
#import "GYNotificationOberverIdentifer.h"
#import <pthread.h>
@interface GYNotificationOberverIdentifer()
@property(nonatomic,strong) NSNotificationCenter *notificationCenterCenter;
@property(nonatomic,strong) dispatch_queue_t dispatchQueue;
@property(nonatomic,copy) GYNotificatioObserverBlock block;
@property(nonatomic,weak) id object;
@end
@implementation GYNotificationOberverIdentifer
- (instancetype)init {
if (self = [super init]) {
}
return self;
}
- (void)congifureWithName:(NSString *)anName
withObject:(id)object
withDispatchQueue:(dispatch_queue_t)dispatchQueue
withBlock:(GYNotificatioObserverBlock)block {
NSAssert(anName,@"name is nil");
NSAssert(block,@"block is nil");
[self clearAll];
_name = anName;
_dispatchQueue = dispatchQueue;
_block = block;
_object = object;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:anName object:object];
}
- (void)dealloc {
[self stopObserver];
}
- (void)handleNotification:(NSNotification *)notification {
if (self.dispatchQueue) {
if ([NSThread isMainThread] && self.dispatchQueue == dispatch_get_main_queue()) {
self.block(notification);
} else {
dispatch_async(self.dispatchQueue, ^{
if (self.block) {
self.block(notification);
}
});
}
} else {
self.block(notification);
}
}
- (void)stopObserver {
[[NSNotificationCenter defaultCenter] removeObserver:self name:_name object:_object];
[self clearAll];
}
- (void)clearAll {
_block = nil;
_object = nil;
_dispatchQueue = nil;
}
@end
@interface GYNotificationOberverIdentifersContainer()
@property(nonatomic,strong) NSMutableDictionary *notificationOberverIdentifersDic;
@end
@implementation GYNotificationOberverIdentifersContainer
- (void)addNotificationOberverIdentifer:(GYNotificationOberverIdentifer *)identifier {
NSAssert(identifier,@"identifier is nil");
if (identifier) {
NotificationPerformLocked(^{
[self modifyContainer:^(NSMutableDictionary *notificationOberverIdentifersDic) {
//不重复add observer
if (![notificationOberverIdentifersDic objectForKey:identifier.name]) {
[notificationOberverIdentifersDic setObject:identifier forKey:identifier.name];
}
}];
});
}
}
- (void)removeObserverWithName:(NSString *)name {
if (name) {
NotificationPerformLocked(^{
[self modifyContainer:^(NSMutableDictionary *notificationOberverIdentifersDic) {
if ([notificationOberverIdentifersDic objectForKey:name]) {
GYNotificationOberverIdentifer *identifier = (GYNotificationOberverIdentifer *)[notificationOberverIdentifersDic objectForKey:name];
[identifier stopObserver];
[notificationOberverIdentifersDic removeObjectForKey:name];
}
}];
});
}
}
- (void)removeObserver {
NotificationPerformLocked(^{
[self modifyContainer:^(NSMutableDictionary *notificationOberverIdentifersDic) {
for (NSString *key in notificationOberverIdentifersDic ) {
GYNotificationOberverIdentifer *identifier = (GYNotificationOberverIdentifer *)[notificationOberverIdentifersDic objectForKey:key];
[identifier stopObserver];
}
[notificationOberverIdentifersDic removeAllObjects];
}];
});
}
- (void)modifyContainer:(void (^)(NSMutableDictionary *notificationOberverIdentifersDic)) block {
if (!self.notificationOberverIdentifersDic) {
self.notificationOberverIdentifersDic = [[NSMutableDictionary alloc] init];
}
block(self.notificationOberverIdentifersDic);
}
#pragma mark -- helper
static void NotificationPerformLocked(dispatch_block_t block) {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
block();
pthread_mutex_unlock(&mutex);
}
@end