如何高效管理AppDelegate中的


导语:

在实际开发过程中,我们常常需要在AppDelegate中监听用户对应用的操作,并做出回应,随着应用的复杂化,AppDelegate中的代码量将非常复杂,非常不宜管理,且容易发生时序性的bug。我们应该如何有效的管理AppDelegate呢?

1. AppDelegate中我们最常做的事

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   ...
    return YES;
}

没错,创建应用时,我们一定会在didFinishLaunchingWithOptions方法中,创建rootViewController和UIWindow。但是,在AppDelegate中,我们能做的还有很多。

2. AppDelegate还能做什么?

- (void)applicationDidEnterBackground:(UIApplication *)application {
   //当视频正在播放时,用户通过home回到手机主界面,我们需要让PlayerManager在此方法中暂停播放。
   //如果我们应用有下载功能,需要在这里实现暂停下载的逻辑。
   //开屏广告需要在这个方法中开启计时,当用户再次回到应用中,通过计时判断是否需要展示开屏广告。
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
   //应用再次回到前端时,有时候会进行一些必要的状态请求。
   //是否需要再次开启视频播放和下载?
   //拉取登录用户信息,检查是否在其他地方登录,有没有修改密码?
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification {
     //推送来了,要不要在应用图标上加上数字呢?
}

- (void)applicationWillTerminate:(UIApplication *)application {
    //应用即将关闭,保存下载进度
    //注销三方库的授权工作
    //需要手动清理一些状态值
}

我们发现,在AppDelegate中,我们会将所有Manager执行方法的入口都堆积在一个地方。这对于代码阅读和管理,以及bug的修改检查都是非常不方便的。那么我们如何解决这个问题呢?

3. 让我看看你有什么能耐?

在理论的代码结构中,我们会将功能性代码封装成一个一个的模块,这些模块是脱离业务逻辑层的。比如说,我们现在有一个广告模块,里面实现的方法有请求广告,缓存广告,用户点击上报等功能。这些都是脱离于业务层的,我们在任何页面添加广告功能,都是需要实现这些方法的。而广告模块也需要在AppDelegate中实现一些广告请求和展示的逻辑。那么两者之间就产生了关联。

4. 解决问题的方法来了!

我们可以再创建一个AppEngine类,继承UIApplicationDelegate,并在Manager中维护一个数组,将需要实现UIApplicationDelegate的类加入数组,统一管理。

Talk is cheap ,show me the code!

首先看看最终的实现结果,我们在AppDelegate中通过创建AppEngine类注册需要实现UIApplicationDelegate的功能模块(xxxManager),之后我们在AppEngine中实现注册模块调用UIApplicationDelegate的逻辑。

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [AppEngine registerManager:[xxxManager new]];
    [AppEngine registerManager:[xxxManager new]];
    
    [[AppEngine shareEngine] application:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}

创建一个管理所有需要实现UIApplicationDelegate方法的功能模块的AppEngine类。

@interface WQAppEngine : NSObject

+ (instancetype)shareEngine;;

/**********************************************************************/
#pragma mark - 模块管理
/**********************************************************************/

+ (void)registerManager:(id)manager;
+ (void)unregisterManager:(id)manager;
+ (id)managerWithClass:(Class)aClass;

@end

我们以didFinishLaunchingWithOptions方法为例,如果某个功能模块

static AppEngine *appEngine = nil;
+ (instancetype)shareEngine{
    static dispatch_once_t t;
    dispatch_once(&t, ^{
        appEngine = [[WQAppEngine alloc] init];
    });
    return appEngine;
}

/**********************************************************************/
#pragma mark - 模块管理
/**********************************************************************/

+ (void)registerManager:(id)manager {
    ...
}

+ (void)unregisterManager:(id)manager {
    ...
}

+ (id)managerWithClass:(Class)aClass {
    for (id manager in engine.managerArray) {
        if ([manager isKindOfClass:aClass]) {
            return manager;
        }
    }
    return nil;
}


/**********************************************************************/
#pragma mark - UIApplicationDelegate
/**********************************************************************/

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions {
    DDLogDebug(@"%s", __FUNCTION__);

    __block BOOL flag = NO;
    [self.managerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)]) {
            BOOL tempFlag = [obj application:application didFinishLaunchingWithOptions:launchOptions];
            if (!flag) {
                flag = tempFlag;
            }
        }
    }];
    return flag;
}

5. 我们为什么不直接让各个模块Manager继承UIApplicationDelegate?

首先,这么做是可以的。但从封装的角度来讲,各个功能模块因为是脱离逻辑层,我们可以将模块封装成库,在任何应用中引入使用的。如果我们将这个库引入新开发App中,但是这个APP现在不需要广告功能,我们只需要在AppDelegatedidFinishLaunchingWithOptions方法中,将注册广告Manager的方法注销掉即可。

我们是如何实现上面一段说的统一管理的?
我们拿广告模块来举例说明,在AppEngine中我们已经实现了方法:

+ (id)managerWithClass:(Class)aClass {
    for (id manager in engine.managerArray) {
        if ([manager isKindOfClass:aClass]) {
            return manager;
        }
    }
    return nil;
}

随后创建一个AppEngine的广告Manager类别,实现一个创建单例的方法:

@implementation AppEngine (Advert)

+ (nullable AdvertManager *)advertManager {
    return [self managerWithClass:[AdvertManager class]];
}
@end

因为AppEngine是单例,所以这里我们也只会创建一次AdvertManager。这样一来,如果我们没有在AppDelegatedidFinishLaunchingWithOptions方法中,注册AdvertManager,那么AdvertManager其实就是为空。

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //[AppEngine registerManager:[AdvertManager new]];
    [AppEngine registerManager:[xxxManager new]];
    
    [[AppEngine shareEngine] application:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}

你可能感兴趣的:(如何高效管理AppDelegate中的)