Skip to content

Commit 4079f83

Browse files
author
Joe Newton
committed
Updated README and podspec for version 1.0.2
1 parent 5071bf1 commit 4079f83

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

KeyValueObservation.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "KeyValueObservation"
4-
s.version = "1.0.1"
4+
s.version = "1.0.2"
55
s.summary = "A small KVO helper library"
66
s.description = <<-DESC
77
A small KVO helper library that provides a NSObject and a NSArray category for observing key value changes using blocks
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818

1919
s.source = { :git => "https://github.com/SomeRandomiOSDev/KeyValueObservation.git", :tag => s.version.to_s }
2020

21-
s.public_header_files = 'KeyValueObservation/NSObject+KeyValueObservation.h', 'KeyValueObservation/NSArray+KeyValueObservation.h', 'KeyValueObservation/SRDKeyValueObservation.h', 'KeyValueObservation/SRDKeyValueObservedChange.h'
21+
s.public_header_files = 'KeyValueObservation/NSObject+KeyValueObservation.h', 'KeyValueObservation/NSArray+KeyValueObservation.h', 'KeyValueObservation/SRDKeyValueObservation.h', 'KeyValueObservation/SRDKeyValueObservedChange.h', 'KeyValueObservation/SRDKVOInfo.h'
2222
s.source_files = 'KeyValueObservation/**/*.{h,m}'
2323
s.frameworks = 'Foundation'
2424
s.requires_arc = true

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ NSObject *observation = [array observeKeyPath:keyPath
7474
}];
7575
```
7676

77+
An additional capability of this library is the ability to easily ignore certain Key-Value Observations for the context of a handler block without any additional overhead. For example:
78+
79+
```objc
80+
@implementation FooBar
81+
82+
...
83+
84+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
85+
if ([keyPath isEqualToString:@"foobar"] && object == self.foobarObject) {
86+
// Some action. Won't ever be called from the context of -[FooBar someMethod] given
87+
// the use of -[NSObject performWhileIgnoringObservations:handler:]
88+
}
89+
else {
90+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
91+
}
92+
}
93+
94+
...
95+
96+
- (void)someMethod {
97+
[self performWhileIgnoringObservations:@[[SRDKVOInfo infoWithObserved:self.foobarObject keyPath:@"foobar"]] handler:^{
98+
self.foobarObject.foobar = @"foobar";
99+
}];
100+
}
101+
102+
@end
103+
```
104+
105+
In lieu of having to add flags to your object to determine when or when not to ignore particular Key-Value Observations one can simply do it from the context of a handler block. Any observations that match any of the `SRDKVOInfo` objects passed in to the method will be automatically ignored while executing the block. After the block finishes executing, the observations are once again passed through as normal.
106+
107+
Note that `-[NSObject performWhileIgnoringObservations:handler:]` uses method implementation swizzling to be able to ignore the specified observations, therefore it is important that you do not do any swizzling for the method `-[NSObject observeValueForKeyPath:ofObject:change:context:]` for the receiving class from the context of the handler block. Doing so could lead to unexpected results.
108+
77109
## Author
78110
79111
Joseph Newton, somerandomiosdev@gmail.com

0 commit comments

Comments
 (0)