最近做模块化遇到一个问题,就是UIApplicationDelegate的一些回调需要在各个模块使用,最后想到了RxSwift的DelegateProxy完美解决。(DelegateProxy以下简称DP)
RxSwift的DP是对系统Delegate的一个rx包装,将delegate的一系列方法转为Observable<[Any]>序列。
先来看看效果
UIApplication.shared.rx
.willResignActive
.bindNext {
print("rx will resign active")
}
.disposed(by: bag)
在每一个需要调用的地方这么实用就行了是不是很爽。
那么Rx的DP的实现原理是什么呢?
DP会先把系统的delegate对象保存一份,是弱引用的assign。然后拦截delegate的方法,系统回调delegate的方法时DP会调用delegate对应的方法,然后DP自己也会调用一次。
《这里有个坑就是 UIApplicationDelegate使用实现的时候,会有问题,因为DP保存的delegate是assign,而我们一般使用delegate的时候,一般delegate是viewcontroller等,viewcontroller再别的地方是被持有的,而appDelegate没有,一旦我们做DP就会导致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 {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationDidBecomeActive(_:)))
.map{ _ in
return .active
}
}
var didEnterBackground: Observable {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationDidEnterBackground(_:)))
.map{ _ in
return .background
}
}
var willResignActive: Observable {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationWillResignActive(_:)))
.map{ _ in
return .inactive
}
}
var willTerminate: Observable {
return delegate
.methodInvoked(#selector(UIApplicationDelegate.applicationWillTerminate(_:)))
.map{ _ in }
}
var state: Observable {
return Observable.of(
didBecomeActive,
didEnterBackground,
willResignActive
)
.merge()
.startWith(base.applicationState)
}
var isFirstLaunch: Observable {
return Observable.just(XPConstant.isFirstLaunch)
}
var isFirstLaunchOfNewVersion: Observable {
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. 如果用历史版本会导致必须在app-terminate前记录历史版本到userdefaults里,这样逻辑分离,代码很不稳定
2. 如果在这里记录会导致另外一个问题就是 如果先调用iFirstLaunch 会影响isFirstLaunchOfNewVersion,永远得不到isFirstLaunchOfNewVersion事件,因为这里已经记录了历史版本
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
}()
}