| Number of watchers on Github | 667 |
| Number of open issues | 0 |
| Average time to close an issue | 22 days |
| Main language | Objective-C |
| Average time to merge a PR | about 10 hours |
| Open pull requests | 0+ |
| Closed pull requests | 2+ |
| Last commit | almost 2 years ago |
| Repo Created | over 3 years ago |
| Repo Last Updated | over 1 year ago |
| Size | 76 KB |
| Organization / Author | postmates |
| Latest Release | v3.0.1 |
| Contributors | 3 |
| Page Updated | 2018-03-12 |
| Do you use PMKVObserver? Leave a review! | |
| View PMKVObserver activity | |
| View on github | |
| Fresh, new opensource launches 🚀🚀🚀 | |
|
Trendy new open source projects in your inbox!
View examples
|
|
PMKVObserver provides a safe block-based wrapper around Key-Value Observing, with APIs for both Obj-C and Swift. Features include:
self without retaining it or dealing with a weak reference.// Observe an object for as long as the object is alive.
_ = KVObserver(object: user, keyPath: \User.fullName) { object, _, _ in
// `object` has the same type as `user`
print("User's full name changed to \(object.fullName)")
}
// Convenience methods for working with the change dictionary
_ = KVObserver(object: user, keyPath: \User.fullName, options: [.old, .new]) { _, change, _ in
// unfortunately we don't know what the type of fullName is, so change uses Any
let old = change.old as? String
let new = change.new as? String
if old != new {
print("User's full name changed to \(new ?? "nil")")
}
}
// Unregistering can be done from within the block, even in an .initial callback
_ = KVObserver(object: user, keyPath: \User.fullName, options: [.initial]) { object, _, kvo in
guard !object.fullName.isEmpty else { return }
print("User's full name is \(object.fullName)")
kvo.cancel()
}
// Or you can unregister externally
let token = KVObserver(object: user, keyPath: \User.fullName) { object, _, _ in
print("User's full name changed to \(object.fullName)")
}
// ... sometime later ...
token.cancel()
// You can also pass an observing object and KVO will be unregistered when that object deallocates
_ = KVObserver(observer: self, object: user, keyPath: \User.fullName) { observer, object, _, _ in
// `observer` has the same type as `self`
observer.nameLabel.text = object.fullName
}
Objective-C provides all the same functionality as Swift, albeit without the strong-typing of the observer/object.
// Observe an object for as long as the object is alive.
[PMKVObserver observeObject:self.user keyPath:@"fullName" options:0
block:^(id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nullable change, PMKVObserver * _Nonnull kvo) {
NSLog(@"User's full name changed to %@", [object fullName]);
}];
// Change dictionary is provided, but without the convenience methods.
[PMKVObserver observeObject:self.user keyPath:@"fullName"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
block:^(id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nullable change, PMKVObserver * _Nonnull kvo) {
NSString *old = change[NSKeyValueChangeOldKey];
NSString *new = change[NSKeyValueChangeNewKey];
if (old != new && (new == nil || ![old isEqualToString:new])) {
NSLog(@"User's full name changed to %@", new);
}
}];
// Unregistering and observing object support is also provided (see Swift examples).
Installing as a framework requires a minimum of iOS 8, OS X 10.9, watchOS 2.0, or tvOS 9.0.
If you install by copying the source into your project, it should work on iOS 7 or later (iOS 6 if you remove KVObserver.swift), and OS X 10.7 or later. Please note that it has not been tested on these versions.
PMKVObserver will compile with Xcode 8.3, but the new KeyPath APIs require Xcode 9 or later.
After installing with any mechanism, you can use this by adding import PMKVObserver (Swift) or @import PMKVObserver; (Objective-C) to your code.
To install using Carthage, add the following to your Cartfile:
github "postmates/PMKVObserver" ~> 3.0
To install using CocoaPods, add the following to your Podfile:
pod 'PMKVObserver', '~> 3.0'
You may also install manually by adding the framework to your workspace, or by adding the 3 files KVObserver.h, KVObserver.m, and (optionally) KVObserver.swift to your project.
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.
KeyPath stuff is usable from 3.2 so there's no reason to require 4. PMVKObserver will still compile with Swift 3.1, but the KeyPath APIs won't be available unless compiling with Swift 3.2.KeyPaths.Change.rawDict and Change.kind non-optional.os_unfair_lock on platforms that support it, otherwise it uses an unfair mutex.cancelled to PMKVObserver.pthread_mutex_ts.Initial release.
KeyPath stuff is usable from 3.2 so there's no reason to require 4. PMVKObserver will still compile with Swift 3.1, but the KeyPath APIs won't be available unless compiling with Swift 3.2.KeyPaths.Change.rawDict and Change.kind non-optional.Use os_unfair_lock on platforms that support it.