Skip to content

Commit cb9b4a9

Browse files
committed
Unit test and fix for block observer multiplexing. Closes issue BlocksKit#132.
1 parent cf30012 commit cb9b4a9

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

BlocksKit/NSObject+BlockObservation.m

+12-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static dispatch_queue_t BKObserverMutationQueue() {
5757
static dispatch_queue_t queue = nil;
5858
static dispatch_once_t token = 0;
5959
dispatch_once(&token, ^{
60-
queue = dispatch_queue_create("org.blockskit.observers.queue", 0);
60+
queue = dispatch_queue_create("us.pandamonia.blockskit.observers", 0);
6161
});
6262
return queue;
6363
}
@@ -84,11 +84,7 @@ - (NSString *)addObserverForKeyPaths:(NSArray *)keyPaths options:(NSKeyValueObse
8484
return token;
8585
}
8686

87-
- (void)addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options task:(BKObservationBlock)task {
88-
[self addObserverForKeyPaths: @[keyPath] identifier: identifier options: options task: (id)task];
89-
}
90-
91-
- (void)addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options task:(BKMultipleObservationBlock)task {
87+
- (void)bk_addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options context:(void *)context task:(id)task {
9288
NSParameterAssert(keyPaths.count);
9389
NSParameterAssert(identifier.length);
9490
NSParameterAssert(task);
@@ -109,12 +105,21 @@ - (void)addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)identi
109105
}];
110106
});
111107

112-
void *context = (options == 0) ? ((keyPaths.count == 1) ? &kBlockObservationNoChangeContext : &kMultipleBlockObservationNoChangeContext) : ((keyPaths.count == 1) ? &kBlockObservationContext : &kMultipleBlockObservationContext);
113108
[keyPaths each:^(NSString *keyPath) {
114109
[self addObserver:newObserver forKeyPath:keyPath options:options context:context];
115110
}];
116111
}
117112

113+
- (void)addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options task:(BKObservationBlock)task {
114+
void *context = (options == 0) ? &kBlockObservationNoChangeContext : &kBlockObservationContext;
115+
[self bk_addObserverForKeyPaths:@[keyPath] identifier:identifier options:options context:context task:task];
116+
}
117+
118+
- (void)addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options task:(BKMultipleObservationBlock)task {
119+
void *context = (options == 0) ? &kMultipleBlockObservationNoChangeContext : &kMultipleBlockObservationContext;
120+
[self bk_addObserverForKeyPaths:keyPaths identifier:identifier options:options context:context task:task];
121+
}
122+
118123
- (void)removeObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier {
119124
NSParameterAssert(keyPath.length);
120125
NSParameterAssert(identifier.length);

Tests/NSObjectBlockObservationTest.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
- (void)testNSNumberKeyValueObservation;
1313
- (void)testNSArrayKeyValueObservation;
1414
- (void)testNSSetKeyValueObservation;
15+
- (void)testMultipleKeyValueObservation;
16+
- (void)testMultipleOnlyOneKeyValueObservation;
17+
1518

1619
@end

Tests/NSObjectBlockObservationTest.m

+26-1
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,30 @@ - (void)testNSSetKeyValueObservation {
122122
STAssertEqualObjects(_subject.members,target,@"members are %@",_subject.members);
123123
STAssertEquals(_total, (NSInteger)2, @"total is %d", _total);
124124
[self removeObserverForKeyPath:@"subject.members" identifier:token];
125-
}
125+
}
126+
127+
128+
- (void)testMultipleKeyValueObservation {
129+
NSString *token = [self addObserverForKeyPaths: @[@"subject.kvc", @"subject.number"] task:^(id obj, NSString *keyPath) {
130+
[(NSObjectBlockObservationTest *)obj action];
131+
}];
132+
NSNumber *number = @1;
133+
[self setValue:@NO forKeyPath:@"subject.kvc"];
134+
[self setValue:number forKeyPath:@"subject.number"];
135+
STAssertFalse(_subject.kvc, @"kvc is NO");
136+
STAssertEquals(_subject.number,number,@"number is %@",_subject.number);
137+
STAssertEquals(_total, (NSInteger)2, @"total is %d", _total);
138+
[self removeObserversWithIdentifier: token];
139+
}
140+
141+
- (void)testMultipleOnlyOneKeyValueObservation {
142+
NSString *token = [self addObserverForKeyPaths: @[@"subject.kvc"] task:^(id obj, NSString *keyPath) {
143+
[(NSObjectBlockObservationTest *)obj action];
144+
}];
145+
[self setValue:@NO forKeyPath:@"subject.kvc"];
146+
STAssertFalse(_subject.kvc, @"kvc is NO");
147+
STAssertEquals(_total, (NSInteger)1, @"total is %d", _total);
148+
[self removeObserversWithIdentifier: token];
149+
}
150+
126151
@end

0 commit comments

Comments
 (0)