Skip to content

Instantly share code, notes, and snippets.

@jamztang
Last active December 19, 2015 19:49
Show Gist options
  • Save jamztang/6009092 to your computer and use it in GitHub Desktop.
Save jamztang/6009092 to your computer and use it in GitHub Desktop.
The simplest KVO+Block wrapper. http://ioscodesnippet.com
/*
* This file is part of the http://ioscodesnippet.com
* (c) Jamz Tang <jamz@jamztang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/*
* Usage:
*
* // Create a property
* @property (nonatomic, strong) JTKeyValueObserver *keyObserver;
*
* // Just use the initializer, and you don't need to deregister any observers
* // because the wrapper does it for you when it's released
*
- (void)viewDidLoad {
[super viewDidLoad];
self.keyObserver = [[JTKeyValueObserver alloc] initWithObject:self
keyPath:@"state"
option:NSKeyValueObservingOptionNew
context:nil
handlerBlock:^(NSObject *object, NSString *keyPath, NSDictionary *change, void *context) {
NSLog(@"%@", [object valueForKeyPath:keyPath]);
}];
}
*/
#import <Foundation/Foundation.h>
typedef void(^JTKVOHandler)(NSObject *object, NSString *keyPath, NSDictionary *change, void *context);
@interface JTKeyValueObserver : NSObject
- (id)initWithObject:(__weak NSObject *)object
keyPath:(NSString *)keyPath
option:(NSKeyValueObservingOptions)options
context:(void *)context
handlerBlock:(JTKVOHandler)handler;
@end
/*
* This file is part of the http://ioscodesnippet.com
* (c) Jamz Tang <jamz@jamztang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "JTKeyValueObserver.h"
@interface JTKeyValueObserver ()
@property (nonatomic, copy) NSString *keyPath;
@property (nonatomic, copy) JTKVOHandler handlerBlock;
@property (nonatomic, unsafe_unretained) NSObject *object; // Weak property released before dealloc kicks in, but we don't really want to retain the owner
@end
@implementation JTKeyValueObserver
- (id)initWithObject:(__weak NSObject *)object
keyPath:(NSString *)keyPath
option:(NSKeyValueObservingOptions)options
context:(void *)context
handlerBlock:(JTKVOHandler)handler {
self = [super init];
if (self) {
self.keyPath = keyPath;
self.object = object;
self.handlerBlock = handler;
[object addObserver:self
forKeyPath:keyPath
options:options
context:context];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (self.handlerBlock) {
self.handlerBlock(self.object, keyPath, change, context);
}
}
- (void)dealloc {
[self.object removeObserver:self forKeyPath:self.keyPath];
}
@end
Pod::Spec.new do |s|
s.name = "JTKeyValueObserver"
s.version = "0.0.1"
s.summary = "Revisiting KVO+Block, the simplest version."
s.homepage = "https://gist.github.com/jamztang/6009092"
s.author = { "Jamz Tang" => "jamz@jamztang.com" }
s.platform = :ios
s.source = { :git => "git://gist.github.com/6009092.git", :tag => '0.0.1' }
s.requires_arc = true
s.ios.deployment_target = '5.0'
s.source_files = '*.{h,m}'
s.license = { :type => 'MIT', :file => 'LICENSE' }
end
Copyright (c) 2013 Jamz Tang <jamz@jamztang.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@openaphid
Copy link

This snippet is actually not correct in real world use cases.

JTKeyValueObserver has no mechanism to remove itself from KVO when the observed object has been deallocated. You will get system warnings about NSKVODeallocateBreak and eventually get crash on deallocation of JTKeyValueObserver as it will access a dangling pointer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment