KVO三板斧
person.addObserver(self, forKeyPath: "name", options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { }
deinit {person.removeObserver(self, forKeyPath: "name") }
RxSwift的KVO封装
1.rx.observe:高效,它是一个 KVO 机制的简单封装。
self.person.rx.observe(String.self, "name").subscribe(onNext: { (change) in
print("observe订阅到了KVO:\(String(describing: change))")
}).disposed(by: disposeBag)
2.rx.observeWeakly:执行效率低一些,它处理weak修饰的对象。
self.person.rx.observeWeakly(String.self, "name")
.subscribe(onNext: { (change) in
print("observeWeakly订阅到了KVO:\(String(describing: change))")
})
.disposed(by: disposeBag)
持续更新ing...
网友评论