-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSObject+TLKVO.m
347 lines (289 loc) · 11.1 KB
/
NSObject+TLKVO.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// NSObject+TLKVO.m
// Flowrate
//
// Created by Nathan Vander Wilt on 10/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "NSObject+TLKVO.h"
#include <objc/runtime.h>
NSString* const TLKVOContext = @"NSObject+TLKVO observation context";
@interface TLKVOProxy : NSObject {
@private
__weak NSObject* observer;
Class observerClass;
NSString* observedKeyPath;
void* originalObserverPtr;
}
+ (NSSet*)findProxiesForObserver:(NSObject*)anObserver
ofClass:(Class)theClass
onObject:(id)observedObject
keyPath:(NSString*)theKeyPath;
- (id)initWithObserver:(NSObject*)theObserver
ofClass:(Class)theObserverClass
onObject:(id)observedObject
keyPath:(NSString*)theKeyPath;
@property (readonly) NSString* observedKeyPath;
- (void)unregister:(id)observedObject;
@end
@implementation NSObject (TLKVO)
- (void)tl_addObserver:(NSObject*)observer
ofClass:(Class)observerClass
forKeyPath:(NSString*)keyPath
options:(NSKeyValueObservingOptions)options
{
NSParameterAssert(observer != nil);
NSParameterAssert(observerClass != nil);
NSParameterAssert(keyPath != nil);
NSSet* existingObservers = [TLKVOProxy findProxiesForObserver:observer
ofClass:observerClass
onObject:self
keyPath:keyPath];
NSAssert1(![existingObservers count], @"Observer is already observing %@", keyPath);
TLKVOProxy* proxyObserver = [[TLKVOProxy alloc] initWithObserver:observer
ofClass:observerClass
onObject:self
keyPath:keyPath];
[self addObserver:proxyObserver forKeyPath:keyPath options:options context:(void*)&TLKVOContext];
}
- (void)tl_removeObserver:(NSObject*)observer
ofClass:(Class)observerClass
forKeyPath:(NSString*)keyPath
{
NSParameterAssert(observer != nil);
NSParameterAssert(observerClass != nil);
NSSet* proxyObservers = [TLKVOProxy findProxiesForObserver:observer
ofClass:observerClass
onObject:self
keyPath:keyPath];
NSAssert([proxyObservers count], @"No matching observers found to remove");
for (TLKVOProxy* proxyObserver in proxyObservers) {
NSString* proxyKeyPath = [proxyObserver observedKeyPath];
[self removeObserver:proxyObserver forKeyPath:proxyKeyPath];
[proxyObserver unregister:self];
}
}
@end
/* NOTE: This global variable maps weak observed objects to strong NSMutableDictionary with registered proxies.
If an observed object disappears, we no longer need to keep its proxies around.
So we use observed objects as weak keys so proxies automatically disappear under GC.
(Under managed memory, the client is expected to remove the observer at a suitable time. */
static NSMapTable* observedObjectProxies = nil;
/* NOTE: If using garbage collection, this global variable maps weak observing objects to a helper object
that will remove all proxy observers registered with itself as it is finalized. */
static NSMapTable* observerCleaners = nil;
typedef void (*ObserveMethod)(id, SEL, NSString*, id, NSDictionary*, void*);
@interface TLKVOCleaner : NSObject {
@private
NSMapTable* proxyObjects;
}
- (void)addProxy:(TLKVOProxy*)newProxy forObject:(NSObject*)observedObject;
- (void)removeProxy:(TLKVOProxy*)oldProxy;
- (NSUInteger)proxyCount;
@end
@interface TLKVOProxy ()
@property (readonly) id <NSObject, NSCopying> registrationKey;
@property (readonly) void* originalObserverPtr;
@property (readonly) Class observerClass;
@end
@implementation TLKVOProxy
@synthesize observedKeyPath;
@synthesize originalObserverPtr;
@synthesize observerClass;
+ (id <NSObject, NSCopying>)proxyKeyForObserver:(NSObject*)anObserver
ofClass:(Class)theClass
onKeyPath:(NSString*)theKeyPath
{
return (theKeyPath ?
[NSString stringWithFormat:@"%@ -> %@-%p", theKeyPath, theClass, anObserver] :
[NSString stringWithFormat:@"%@-%p", theClass, anObserver]);
}
+ (NSSet*)findProxiesForObserver:(NSObject*)anObserver ofClass:(Class)theClass
onObject:(id)observedObject keyPath:(NSString*)keyPath
{
NSParameterAssert(anObserver != nil);
NSParameterAssert(theClass != nil);
NSParameterAssert(observedObject != nil);
if (keyPath) { // optimize the known-keyPath case
TLKVOProxy* proxy = nil;
id proxyKey = [self proxyKeyForObserver:anObserver ofClass:theClass onKeyPath:keyPath];
@synchronized (self) {
NSDictionary* proxies = [observedObjectProxies objectForKey:observedObject];
proxy = [proxies objectForKey:proxyKey];
[[proxy retain] autorelease];
}
// NOTE: proxy may act as termination sentinel
return [NSSet setWithObjects:proxy, nil];
}
else { // search for all matching proxies
NSMutableSet* matchingProxies = [NSMutableSet set];
id proxySuffix = [self proxyKeyForObserver:anObserver ofClass:theClass onKeyPath:nil];
@synchronized (self) {
NSDictionary* proxies = [observedObjectProxies objectForKey:observedObject];
for (NSString* proxyKey in proxies) {
if ([proxyKey hasSuffix:proxySuffix]) {
TLKVOProxy* proxy = [proxies objectForKey:proxyKey];
[matchingProxies addObject:proxy];
}
}
}
return matchingProxies;
}
}
+ (void)registerProxy:(TLKVOProxy*)theProxy onObject:(id)observedObject {
NSParameterAssert(theProxy != nil);
NSParameterAssert(observedObject != nil);
id proxyKey = [theProxy registrationKey];
@synchronized (self) {
if (!observedObjectProxies) {
observedObjectProxies = [[NSMapTable mapTableWithWeakToStrongObjects] retain];
}
if ([NSGarbageCollector defaultCollector] && !observerCleaners) {
observerCleaners = [[NSMapTable mapTableWithWeakToStrongObjects] retain];
}
NSMutableDictionary* proxies = [observedObjectProxies objectForKey:observedObject];
if (!proxies) {
proxies = [NSMutableDictionary dictionary];
[observedObjectProxies setObject:proxies forKey:observedObject];
}
if (observerCleaners) {
NSObject* theObserver = theProxy->observer;
TLKVOCleaner* cleaner = [observerCleaners objectForKey:theObserver];
if (!cleaner) {
cleaner = [TLKVOCleaner new];
[observerCleaners setObject:cleaner forKey:theObserver];
[cleaner release];
}
[cleaner addProxy:theProxy forObject:observedObject];
}
NSAssert1(![proxies objectForKey:proxyKey], @"Observer is already observing %@", [theProxy observedKeyPath]);
[proxies setObject:theProxy forKey:proxyKey];
//NSLog(@"Registered '%@' - %@\n", proxyKey, observedObjectProxies);
}
}
+ (void)unregisterProxy:(TLKVOProxy*)theProxy onObject:(id)observedObject {
NSParameterAssert(theProxy != nil);
NSParameterAssert(observedObject != nil);
id proxyKey = [theProxy registrationKey];
@synchronized (self) {
NSMutableDictionary* proxies = [observedObjectProxies objectForKey:observedObject];
NSAssert1([proxies objectForKey:proxyKey], @"Observer was not observing %@", [theProxy observedKeyPath]);
[proxies removeObjectForKey:proxyKey];
if (![proxies count]) {
[observedObjectProxies removeObjectForKey:observedObject];
}
if (observerCleaners) {
NSObject* theObserver = theProxy->observer;
TLKVOCleaner* cleaner = [observerCleaners objectForKey:theObserver];
[cleaner removeProxy:theProxy];
if (![cleaner proxyCount]) {
[observerCleaners removeObjectForKey:theObserver];
}
}
//NSLog(@"Unregistered '%@' - %@\n", proxyKey, observedObjectProxies);
}
}
- (id)initWithObserver:(NSObject*)theObserver
ofClass:(Class)theObserverClass
onObject:(id)observedObject
keyPath:(NSString*)theKeyPath
{
NSParameterAssert(theObserver != nil);
NSParameterAssert(theObserverClass != nil);
NSParameterAssert(observedObject != nil);
NSParameterAssert(theKeyPath != nil);
self = [super init];
if (self) {
/* NOTE: We may need to unregister after our observer reference is zeroed by the garbage collector.
We keep the originalObserverPtr so that we can always create a unique but stable registrationKey. */
observer = originalObserverPtr = theObserver;
observerClass = theObserverClass;
observedKeyPath = [theKeyPath copy];
[[self class] registerProxy:self onObject:observedObject];
}
return self;
}
- (void)unregister:(id)observedObject {
NSParameterAssert(observedObject != nil);
[[self class] unregisterProxy:self onObject:observedObject];
}
- (id <NSObject, NSCopying>)registrationKey {
return [[self class] proxyKeyForObserver:originalObserverPtr ofClass:observerClass onKeyPath:observedKeyPath];
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if (context == &TLKVOContext) {
NSAssert2([keyPath isEqualToString:observedKeyPath],
@"Unexpected key path observed (Saw change for %@, instead of %@)", keyPath, observedKeyPath);
if (!observer) {
//NSLog(@"Observation for collected observer should be removed shortly, ignoring change");
return;
}
ObserveMethod doObserve = (ObserveMethod)class_getMethodImplementation(observerClass, _cmd);
doObserve(observer, _cmd, keyPath, object, change, context);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
@implementation TLKVOCleaner
- (id)init {
NSAssert([NSGarbageCollector defaultCollector],
@"KVO cleanup helper should never be used without garbage collection on");
self = [super init];
if (self) {
proxyObjects = (id)CFRetain([NSMapTable mapTableWithStrongToWeakObjects]);
}
return self;
}
- (void)dealloc {
// this class should never be used without garbage collection on
[self doesNotRecognizeSelector:_cmd];
[proxyObjects release];
[super dealloc];
}
- (void)finalize {
for (TLKVOProxy* removedProxy in proxyObjects) {
id observedObject = [proxyObjects objectForKey:removedProxy];
[observedObject tl_removeObserver:[removedProxy originalObserverPtr]
ofClass:[removedProxy observerClass]
forKeyPath:[removedProxy observedKeyPath]];
}
CFRelease(proxyObjects);
[super finalize];
}
- (void)addProxy:(TLKVOProxy*)newProxy forObject:(NSObject*)observedObject {
[proxyObjects setObject:observedObject forKey:newProxy];
}
- (void)removeProxy:(TLKVOProxy*)oldProxy {
[proxyObjects removeObjectForKey:oldProxy];
}
- (NSUInteger)proxyCount {
return [proxyObjects count];
}
@end
// expects method name to be formatted as __func__ does
static Class ClassFromMethodName(const char* method) {
NSString* methodString = [NSString stringWithUTF8String:method];
NSCharacterSet* separators = [NSCharacterSet characterSetWithCharactersInString:@"[ ]"];
NSArray* methodComponents = [methodString componentsSeparatedByCharactersInSet:separators];
NSCAssert([methodComponents count] == 4, @"Must be called with properly formatted method name");
NSString* className = [methodComponents objectAtIndex:1];
return NSClassFromString(className);
}
void TLKVO_RegisterInternal(NSObject* observer, const char* method,
NSObject* targetObject, NSString* keyPath, NSKeyValueObservingOptions options)
{
[targetObject tl_addObserver:observer ofClass:ClassFromMethodName(method)
forKeyPath:keyPath options:options];
}
void TLKVO_UnregisterInternal(NSObject* observer, const char* method,
NSObject* targetObject, NSString* keyPath)
{
[targetObject tl_removeObserver:observer ofClass:ClassFromMethodName(method)
forKeyPath:keyPath];
}