作為了iOS project自帶的life cycle管理類鸳惯,同時(shí)也是原生的一個(gè)singleton類稿械,AppDelegate其實(shí)很多時(shí)候是簡(jiǎn)直就是bad code的典范胜宇。很多人會(huì)直接把所有需要持續(xù)性存在的方法和屬性全部塞到這個(gè)類里面袱结,使得很多項(xiàng)目的AppDelegate都有大幾白行甚至上千行,這個(gè)給代碼管理和迭代帶來(lái)了巨大的困難某弦。作為Tech Lead解決這個(gè)問(wèn)題的方法就是強(qiáng)迫大家遵循一定的設(shè)計(jì)原則桐汤,在實(shí)際工作中,我提出的原則就是:“誰(shuí)污染誰(shuí)治理” - 每一個(gè)模塊或者方法的作者有義務(wù)自行管理模塊或者方法的life cycle靶壮。具體實(shí)現(xiàn)方法就是通過(guò)將跟App Life Cycle相關(guān)的方法和模塊都抽象成不同的服務(wù)來(lái)插入原有的AppDelegate之中怔毛。這樣的好處是代碼管理和清理難度大幅度降低,同時(shí)代碼的可讀性大幅度上升腾降。
首先設(shè)立一個(gè)placeholder來(lái)繼承UIApplicationDelegate
import UIKit
protocol ApplicationService:UIApplicationDelegate {}
class LifeCycleService:UIResponder,ApplicationService {
var window: UIWindow?
//Why computed? For AppDelegate to override. It should not be computed property if you don't adopt this structure.
var services:[ApplicationService] {
return []
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
for service in services {
service.applicationDidFinishLaunching?(application)
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
for service in services {
service.applicationWillResignActive?(application)
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
for service in services {
service.applicationDidEnterBackground?(application)
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
for service in services {
service.applicationWillEnterForeground?(application)
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
for service in services {
service.applicationDidBecomeActive?(application)
}
}
func applicationWillTerminate(_ application: UIApplication) {
for service in services {
service.applicationWillTerminate?(application)
}
}
/* To be continued...
I know you don't like the word technologically, so theoretically, you should (another bad word here) implement all methods in the UIApplicationDelegate!!! Why I don't do it? Yes, you are right, because I am lazy. Although lazy is a good word to describe a developer.
*/
}
這只是一個(gè)例子拣度,實(shí)際上,你可以根據(jù)自己需要,把所有UIApplicationDelegate里面的方法全部用這種方法實(shí)現(xiàn)蜡娶。
然后再根據(jù)自己的需求來(lái)設(shè)立想要的Service模塊:
import UIKit
class DependencyLoading:NSObject, ApplicationService {
func applicationDidFinishLaunching(_ application: UIApplication) {
//Let's pretend to have a Reachability class
Reachability.shared.checkInternetAccessbility()
}
func applicationWillResignActive(_ application: UIApplication) {
Reachability.shared.stopMonitotingReachability()
}
func applicationDidBecomeActive(_ application: UIApplication) {
Reachability.shared.startMonitoringReachability()
}
}
extension DependencyLoading:ReachabilityDelegate {
func didLostInternetConnection() {
/*
Please Remember this name, next time anything breaks including you forgot your AD-ENT password, make sure you call
this guy before step 2.
*/
print("Siri, call Andrew Martinez...")
}
}
你可以用這種方法把各種不同的功能抽象成不同的模塊混卵,比如Push Notification 相關(guān)的方法和屬性就可以抽象成NotificationService。LocalNotification的方法抽象成LocationNotificationService窖张。CoreData相關(guān)的方法和屬性抽象成PersistentService之類的幕随。
最后我們來(lái)看一下系統(tǒng)自帶的AppDelegate:
import UIKit
@UIApplicationMain
class AppDelegate: LifeCycleService {
override var services: [ApplicationService] {
return [ /*
Here is checkin list and checkin is mandatory for every one!
*/
LifeCycleService(),
DependencyLoading()
]
}
}