最近做模塊化遇到一個問題,就是UIApplicationDelegate的一些回調(diào)需要在各個模塊使用,最后想到了RxSwift的DelegateProxy完美解決。(DelegateProxy以下簡稱DP)
RxSwift的DP是對系統(tǒng)Delegate的一個rx包裝,將delegate的一系列方法轉(zhuǎn)為Observable<[Any]>序列。
先來看看效果
UIApplication.shared.rx
.willResignActive
.bindNext {
print("rx will resign active")
}
.disposed(by: bag)
在每一個需要調(diào)用的地方這么實用就行了是不是很爽。
那么Rx的DP的實現(xiàn)原理是什么呢?
DP會先把系統(tǒng)的delegate對象保存一份,是弱引用的assign。然后攔截delegate的方法,系統(tǒng)回調(diào)delegate的方法時DP會調(diào)用delegate對應(yīng)的方法,然后DP自己也會調(diào)用一次。
《這里有個坑就是 UIApplicationDelegate使用實現(xiàn)的時候,會有問題,因為DP保存的delegate是assign,而我們一般使用delegate的時候,一般delegate是viewcontroller等,viewcontroller再別的地方是被持有的,而appDelegate沒有,一旦我們做DP就會導(dǎo)致appDelegate被釋放了,可以在appDelegate打斷點觀察,所以下面的源碼重載了DP的一個方法對這種特殊情況特殊處理,也就是強引用了appDelegate》。
下面是完整源碼:
import RxCocoa
import RxSwift
private class RxUIApplicationDelegateProxy: DelegateProxy, UIApplicationDelegate, DelegateProxyType {
static func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
let app: UIApplication = object as! UIApplication
app.delegate = delegate as? UIApplicationDelegate
}
static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
let app: UIApplication = object as! UIApplication
return app.delegate
}
//對于appDelegate這里必須強引用,不然appDelegate就被釋放了。
override func setForwardToDelegate(_ delegate: AnyObject?, retainDelegate: Bool) {
super.setForwardToDelegate(delegate, retainDelegate: true)
}
}
extension Reactive where Base: UIApplication {
var delegate: DelegateProxy {
return RxUIApplicationDelegateProxy.proxyForObject(base)
}
var didBecomeActive: Observable<UIApplicationState> {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationDidBecomeActive(_:)))
.map{ _ in
return .active
}
}
var didEnterBackground: Observable<UIApplicationState> {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationDidEnterBackground(_:)))
.map{ _ in
return .background
}
}
var willResignActive: Observable<UIApplicationState> {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationWillResignActive(_:)))
.map{ _ in
return .inactive
}
}
var willTerminate: Observable<Void> {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationWillTerminate(_:)))
.map{ _ in }
}
var state: Observable<UIApplicationState> {
return Observable.of(
didBecomeActive,
didEnterBackground,
willResignActive
)
.merge()
.startWith(base.applicationState)
}
var isFirstLaunch: Observable<Bool> {
return Observable.just(XPConstant.isFirstLaunch)
}
var isFirstLaunchOfNewVersion: Observable<Bool> {
return Observable.just(XPConstant.isFirstLaunchOfNewVersion)
}
}
fileprivate struct XPConstant {
struct Key {
static var appHadLaunch: String {
return "xp_appHadLaunch"
}
static var appFirstLaunchOfNewVersion: String {
return "xp_appFirstLaunchOfNewVersionn"
}
static var lastVersionOfAppLaunch: String {
return "xp_lastVersionAppLaunch"
}
}
static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
static let isFirstLaunch: Bool = {
/** 這里單用一個key來判斷處于以下原因
1. 如果用歷史版本會導(dǎo)致必須在app-terminate前記錄歷史版本到userdefaults里,這樣邏輯分離,代碼很不穩(wěn)定
2. 如果在這里記錄會導(dǎo)致另外一個問題就是 如果先調(diào)用iFirstLaunch 會影響isFirstLaunchOfNewVersion,永遠(yuǎn)得不到isFirstLaunchOfNewVersion事件,因為這里已經(jīng)記錄了歷史版本
3. ***最重要的是為了保持功能的獨立性,即便你不使用isFirstLaunchOfNewVerion也不影響該功能***
*/
let userDefaults = UserDefaults.standard
let isFirstLaunch = !userDefaults.bool(forKey: Key.appHadLaunch)
userDefaults.set(true, forKey: Key.appHadLaunch)
userDefaults.synchronize()
return isFirstLaunch
}()
static let isFirstLaunchOfNewVersion: Bool = {
let userDefaults = UserDefaults.standard
let lastLaunchVersion: String? = userDefaults.string(forKey: Key.lastVersionOfAppLaunch)
let currentVersion = Constant.appVersion
let isFirstLaunchOfNewVersion = lastLaunchVersion != currentVersion
userDefaults.set(currentVersion, forKey: Key.lastVersionOfAppLaunch)
userDefaults.synchronize()
return isFirstLaunchOfNewVersion
}()
}