学习笔记
2017-08-17 本文已影响0人
夜雨聲煩_
模块化
涉及到的类
- 底层:UPCore
- 模块管理:moduleManager
- 协议:UPModuleProtocol
- 项目入口:APPDelegate
- 具体模块:UPPush
逻辑关系
(+ load 作为 Objective-C 中的一个方法,与其它方法有很大的不同。它只是一个在整个文件被加载到运行时,在 main 函数调用之前被 ObjC 运行时调用的钩子方法)
- 模块向UPCore注册
UPPushModule.h
+ (void)load
{
[UPCore registerModuleClass:[self class]];
}
2.在UPCore中初始化并调用UPModuleManager
+ (void)registerModuleClass:(Class)moduleClass
{
[[UPModuleManager sharedInstance] registerModuleClass:moduleClass];
}
3.初始化单例UPModuleManager,初始化字典
+ (instancetype)sharedInstance
{
static dispatch_once_t predicate;
static id UPModuleManagerInstance = nil;
dispatch_once(&predicate, ^{
UPModuleManagerInstance = [[self alloc] init];
});
return UPModuleManagerInstance;
}
- (instancetype)init
{
if (self = [super init]) {
_modules = [NSMutableArray array];
_moduleEventSelectorInfoDictionary = @{ @(UPMSetupEvent) : kUPModuleSetupSelector,
@(UPMInitEvent) : kUPModuleInitSelector,
@(UPMTearDownEvent) : kUPModuleTearDownSelector,
@(UPMWillResignActiveEvent) : kUPModuleWillResignActiveSelector,
@(UPMDidEnterBackgroundEvent) : kUPModuleDidEnterBackgroundSelector,
@(UPMWillEnterForegroundEvent) : kUPModuleWillEnterForegroundSelector,
@(UPMDidBecomeActiveEvent) : kUPModuleDidBecomeActiveSelector,
@(UPMWillTerminateEvent) : kUPModuleWillTerminateSelector,
@(UPMDidReceiveMemoryWarningEvent) : kUPModuleDidReceiveMemoryWarningSelector,
@(UPMDidFailToRegisterForRemoteNotificationsEvent) : kUPModuleDidFailToRegisterForRemoteNotificationsSelector,
@(UPMDidRegisterForRemoteNotificationsEvent) : kUPModuleDidRegisterForRemoteNotificationsSelector,
@(UPMDidReceiveRemoteNotificationEvent) : kUPModuleDidReceiveRemoteNotificationSelector,
@(UPMDidReceiveLocalNotificationEvent) : kUPModuleDidReceiveLocalNotificationSelector,
@(UPMDidRegisterUserNotificationSettings) : kUPMDidRegisterUserNotificationSettingsSelector };
}
return self;
}
- (void)registerModuleClass:(Class)moduleClass
{
if (moduleClass == nil) {
UPLogError(@"CoreHive", @"%s[%d]模块注册失败!该模块类为nil!", __PRETTY_FUNCTION__, __LINE__);
return;
}
if (![moduleClass conformsToProtocol:@protocol(UPModuleProtocol)]) {
UPLogError(@"CoreHive", @"%s[%d]模块注册失败!该模块类未实现UPModuleProtocol协议!", __PRETTY_FUNCTION__, __LINE__);
return;
}
NSString *moduleClassName = NSStringFromClass(moduleClass);
if (![moduleClassName isKindOfClass:[NSString class]] || moduleClassName.length == 0) {
UPLogError(@"CoreHive", @"%s[%d]模块注册失败!该模块类为空!", __PRETTY_FUNCTION__, __LINE__);
return;
}
if ([self.modules containsObject:moduleClassName]) {
UPLogWarning(@"CoreHive", @"%s[%d]该模块类(%@)已注册过!", __PRETTY_FUNCTION__, __LINE__, moduleClassName);
return;
}
[self.modules addObject:moduleClassName];
}
4.使用const声明的selector对应value,使用枚举声明key
UPModuleManager.m
NSString *kUPModuleClassName = @"moduleClass";
NSString *const kUPModuleSetupSelector = @"moduleSetup:";
NSString *const kUPModuleInitSelector = @"moduleInit:";
NSString *const kUPModuleTearDownSelector = @"moduleTearDown:";
NSString *const kUPModuleWillResignActiveSelector = @"moduleWillResignActive:";
NSString *const kUPModuleDidEnterBackgroundSelector = @"moduleDidEnterBackground:";
NSString *const kUPModuleWillEnterForegroundSelector = @"moduleWillEnterForeground:";
NSString *const kUPModuleDidBecomeActiveSelector = @"moduleDidBecomeActive:";
NSString *const kUPModuleWillTerminateSelector = @"moduleWillTerminate:";
NSString *const kUPModuleDidReceiveMemoryWarningSelector = @"moduleDidReceiveMemoryWarning:";
NSString *const kUPModuleDidFailToRegisterForRemoteNotificationsSelector = @"moduleDidFailToRegisterForRemoteNotifications:";
NSString *const kUPModuleDidRegisterForRemoteNotificationsSelector = @"moduleDidRegisterForRemoteNotifications:";
NSString *const kUPModuleDidReceiveRemoteNotificationSelector = @"moduleDidReceiveRemoteNotification:";
NSString *const kUPModuleDidReceiveLocalNotificationSelector = @"moduleDidReceiveLocalNotification:";
NSString *const kUPMDidRegisterUserNotificationSettingsSelector = @"moduleDidRegisterUserNotificationSettings:";
UPModuleManager.h
/* 模块事件枚举值 */
typedef NS_ENUM(NSInteger, UPModuleEventType) {
UPMSetupEvent = 0,
UPMInitEvent,
UPMTearDownEvent,
UPMWillResignActiveEvent,
UPMDidEnterBackgroundEvent,
UPMWillEnterForegroundEvent,
UPMDidBecomeActiveEvent,
UPMWillTerminateEvent,
UPMDidReceiveMemoryWarningEvent,
UPMDidFailToRegisterForRemoteNotificationsEvent,
UPMDidRegisterForRemoteNotificationsEvent,
UPMDidReceiveRemoteNotificationEvent,
UPMDidReceiveLocalNotificationEvent,
UPMDidRegisterUserNotificationSettings
};
5.进入main函数,进入UPAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[[UPModuleManager sharedInstance] handleModuleEvent:UPMSetupEvent];
[[UPModuleManager sharedInstance] handleModuleEvent:UPMInitEvent];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif
return YES;
}
6.进入处理方法
- (void)handleModuleEvent:(UPModuleEventType)eventType
{
NSString *selectorName = _moduleEventSelectorInfoDictionary[@(eventType)];
if (![selectorName isKindOfClass:[NSString class]]) {
return;
}
SEL selector = NSSelectorFromString(selectorName);
if (!selector) {
return;
}
[self.modules enumerateObjectsUsingBlock:^(id<UPModuleProtocol> moduleInstance, NSUInteger idx, BOOL *_Nonnull stop) {
if ([moduleInstance respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[moduleInstance performSelector:selector
withObject:[UPContext sharedInstance]];
#pragma clang diagnostic pop
}
}];
}
7.补充协议内容
#import <Foundation/Foundation.h>
@class UPContext;
/**
* 模块协议声明接口。该接口声明了做为一个模块,都需要时间的方法。
*/
@protocol UPModuleProtocol <NSObject>
@optional
/**
* @brief 模块Setup方法。
* @param context 包含CoreHive环境信息的对象.
* @discussion 模块可以在该方法中去进一步注册其对外提供的服务。
*/
- (void)moduleSetup:(UPContext *)context;
/**
* @brief 模块初始化方法。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleInit:(UPContext *)context;
/**
* @brief 模块销毁方法。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleTearDown:(UPContext *)context;
/**
* @brief 当App处于非活跃状态时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleWillResignActive:(UPContext *)context;
/**
* @brief 当App已经进入后台时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidEnterBackground:(UPContext *)context;
/**
* @brief 当App将要进入后台时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleWillEnterForeground:(UPContext *)context;
/**
* @brief 当App处于活跃状态时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidBecomeActive:(UPContext *)context;
/**
* @brief 当App将要退出时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleWillTerminate:(UPContext *)context;
/**
* @brief 当App收到内存告警时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidReceiveMemoryWarning:(UPContext *)context;
/**
* @brief 当App远程推送通知注册失败时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidFailToRegisterForRemoteNotifications:(UPContext *)context;
/**
* @brief 当App远程推送通知注册成功时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidRegisterForRemoteNotifications:(UPContext *)context;
/**
* @brief 当App收到远程推送通知时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidReceiveRemoteNotification:(UPContext *)context;
/**
* @brief 当App收到本地通知时,CoreHive通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidReceiveLocalNotification:(UPContext *)context;
/**
* @brief 当App收到application:didRegisterUserNotificationSettings:代理方法时,CoreHive会通过该方法通知给模块。
* @param context 包含CoreHive环境信息的对象.
*/
- (void)moduleDidRegisterUserNotificationSettings:(UPContext *)context __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
@end
这样,所有的模块都在UPAppDelegate中调度,以UPModuleProtocol为核心,所有遵循此协议的模块都可以操作。搭建了项目的根本,包括推送日志登录等功能,不包括设备绑定等具体业务逻辑。