Skip to content

Commit 5b386f9

Browse files
Yusuke Onishikzaher
Yusuke Onishi
authored andcommitted
Add wrappers for observe methods to use Swift 4 key path
1 parent 95a51a6 commit 5b386f9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

RxCocoa/Foundation/NSObject+Rx.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ extension Reactive where Base: NSObject {
6868
public func observe<E>(_ type: E.Type, _ keyPath: String, options: KeyValueObservingOptions = [.new, .initial], retainSelf: Bool = true) -> Observable<E?> {
6969
return KVOObservable(object: base, keyPath: keyPath, options: options, retainTarget: retainSelf).asObservable()
7070
}
71+
72+
/**
73+
Observes values on `keyPath` starting from `self` with `options` and retains `self` if `retainSelf` is set.
74+
75+
`observe` is just a simple and performant wrapper around KVO mechanism.
76+
77+
* it can be used to observe paths starting from `self` or from ancestors in ownership graph (`retainSelf = false`)
78+
* it can be used to observe paths starting from descendants in ownership graph (`retainSelf = true`)
79+
* the paths have to consist only of `strong` properties, otherwise you are risking crashing the system by not unregistering KVO observer before dealloc.
80+
81+
If support for weak properties is needed or observing arbitrary or unknown relationships in the
82+
ownership tree, `observeWeakly` is the preferred option.
83+
84+
- parameter keyPath: Key path of property names to observe.
85+
- parameter options: KVO mechanism notification options.
86+
- parameter retainSelf: Retains self during observation if set `true`.
87+
- returns: Observable sequence of objects on `keyPath`.
88+
*/
89+
public func observe<E>(_ keyPath: KeyPath<Base, E>, options: KeyValueObservingOptions = [.new, .initial], retainSelf: Bool = true)-> Observable<E?> {
90+
return observe(E.self, keyPath._kvcKeyPathString!, options: options, retainSelf: retainSelf)
91+
}
7192
}
7293

7394
#endif
@@ -95,6 +116,24 @@ extension Reactive where Base: NSObject {
95116
return n as? E
96117
}
97118
}
119+
120+
/**
121+
Observes values on `keyPath` starting from `self` with `options` and doesn't retain `self`.
122+
123+
It can be used in all cases where `observe` can be used and additionally
124+
125+
* because it won't retain observed target, it can be used to observe arbitrary object graph whose ownership relation is unknown
126+
* it can be used to observe `weak` properties
127+
128+
**Since it needs to intercept object deallocation process it needs to perform swizzling of `dealloc` method on observed object.**
129+
130+
- parameter keyPath: Key path of property names to observe.
131+
- parameter options: KVO mechanism notification options.
132+
- returns: Observable sequence of objects on `keyPath`.
133+
*/
134+
public func observeWeakly<E>(_ keyPath: KeyPath<Base, E>, options: KeyValueObservingOptions = [.new, .initial])-> Observable<E?> {
135+
return observeWeakly(E.self, keyPath._kvcKeyPathString!, options: options)
136+
}
98137
}
99138
#endif
100139

0 commit comments

Comments
 (0)