目錄
簡(jiǎn)述
組件化路由的設(shè)計(jì)方案,用于組件化各業(yè)務(wù)模塊的解耦
您的star其爵,是我前進(jìn)的動(dòng)力 ??
MTRouter地址
功能:
- 直接配置Controller,實(shí)現(xiàn)Controller的創(chuàng)建伸蚯,push摩渺,present
- 配置Handler,用戶模塊個(gè)性化配置剂邮、對(duì)已有Controller實(shí)例參數(shù)傳遞
設(shè)計(jì)結(jié)構(gòu)
NSURLComponent介紹
首先來(lái)熟悉一下NSURLComponent的各個(gè)字段:
URL結(jié)構(gòu)
路由對(duì)象存儲(chǔ)設(shè)計(jì)方案
{
module1 = {
firstCtrl = "type: MTRouterModelTypeController, ctrlCls: FirstViewController";
moduleInit = "type: MTRouterModelTypeHandler";
secondCtrl = "type: MTRouterModelTypeController, ctrlCls: SecondViewController";
};
module2 = {
firstCtrl/ttt = "type: MTRouterModelTypeController, ctrlCls: FirstViewController";
moduleInit = "type: MTRouterModelTypeHandler";
secondCtrl/abc = "type: MTRouterModelTypeController, ctrlCls: SecondViewController";
};
}
- 分為兩層的
dictionary
摇幻,第一層Key為Scheme
,第二層Key為host+path
, 存放MTRouterModel
對(duì)象 - 傳參只能通過(guò)
query
和parameters
绰姻,舍棄了module://ctrl/:id
的傳遞方式枉侧,提高查詢效率
類圖結(jié)構(gòu)
類圖
MTRouter
- 用于路由注冊(cè)
-
registerUrl:handler:
注冊(cè)一個(gè)路由,觸發(fā)handler事件狂芋,通常來(lái)說(shuō)用于模塊初始化設(shè)置榨馁,模塊Controller實(shí)例之間參數(shù)傳遞, 不建議亂用 -
registerUrl:controllerCls:
注冊(cè)一個(gè)controller的路由 -
executeUrl:parameters:
、executeUrl:
觸發(fā)一個(gè)路由,當(dāng)是Controller的路由時(shí)帜矾,會(huì)返回Controller -
pushUrl:parameters:animated:pushNavCtrl:
翼虫、pushUrl:animated:pushNavCtrl:
push出url對(duì)應(yīng)的Controller -
presentUrl:parameters:animated:presentCtrl:
、presentUrl:animated:presentCtrl:
present出url對(duì)應(yīng)的Controller -
dictPrint
打印存儲(chǔ)結(jié)構(gòu)
MTRequest
- 自定義request對(duì)象黍特,用于觸發(fā)路由時(shí)蛙讥,解析url
MTURL
-
NSURL
的擴(kuò)展,添加了absolutePath
為host+path
MTRouterNoFoundController
- 當(dāng)找不到路由對(duì)應(yīng)的Controller時(shí)灭衷,返回此類次慢,提示開發(fā)者路由未找到
補(bǔ)充
1.當(dāng)url不存在時(shí),返回MTRouterNoFoundController的實(shí)例翔曲。
2.當(dāng)url已經(jīng)注冊(cè)時(shí)迫像,通過(guò) NSAssert
拋出異常
使用方式
- pod導(dǎo)入
pod 'MTTheme'
- 路由注冊(cè)
大家可以將路由注冊(cè)寫在+load
中, 這樣在pre-main
時(shí)就會(huì)自動(dòng)注冊(cè)路由
// controller路由注冊(cè)
[MTRouter.router registerUrl:@"module://firstCtrl" controllerCls:FirstViewController.class];
[MTRouter.router registerUrl:@"module://secondCtrl" controllerCls:SecondViewController.class];
//模塊個(gè)性化路由注冊(cè)
[MTRouter.router registerUrl:@"module://moduleInit#user" handler:^id(NSDictionary *parameters) {
Initialization *initObj = Initialization.initObj;
initObj.firstCtrlBackgroundColor = parameters[@"firstCtrlBackgroundColor"];
initObj.secondCtrlBackgroundColor = parameters[@"secondCtrlBackgroundColor"];
return nil;
}];
- 路由使用
// controller路由的使用
[MTRouter.router pushUrl:@"module://firstCtrl" animated:YES pushNavCtrl:self.navigationController];
[MTRouter.router presentUrl:@"module://secondCtrl" animated:YES presentCtrl:self];
// 事件路由的使用
[MTRouter.router executeUrl:@"module://moduleInit"
parameters:@{
@"firstCtrlBackgroundColor": [UIColor purpleColor],
@"secondCtrlBackgroundColor": [UIColor orangeColor]
}];
路由性能評(píng)測(cè)
- 環(huán)境:
設(shè)備:iphone6 plus
系統(tǒng):iOS 11.3
- 添加2000個(gè)路由
- 通過(guò)添加
CFAbsoluteTimeGetCurrent()
計(jì)算加載和查詢路由運(yùn)行時(shí)間
//路由注冊(cè)
CFAbsoluteTime routerRegStartTime = CFAbsoluteTimeGetCurrent();
[self registerRouter];
CFAbsoluteTime routerRegEndTime = CFAbsoluteTimeGetCurrent();
NSLog(@"[During]路由注冊(cè)事件 during in %f seconds.", (routerRegStartTime - routerRegEndTime));
//路由查詢handler
CFAbsoluteTime routerHandlerSearchStartTime = CFAbsoluteTimeGetCurrent();
[self moduleInit];
CFAbsoluteTime routerHandlerSearchEndTime = CFAbsoluteTimeGetCurrent();
NSLog(@"[During]路由查詢handler事件 during in %f seconds.", (routerHandlerSearchStartTime - routerHandlerSearchEndTime));
//路由查詢Ctrl
CFAbsoluteTime routerCtrlSearchStartTime = CFAbsoluteTimeGetCurrent();
[MTRouter.router pushUrl:@"module://firstCtrl" animated:YES pushNavCtrl:self.navigationController];
CFAbsoluteTime routerCtrlSearchEndTime = CFAbsoluteTimeGetCurrent();
NSLog(@"[During]路由查詢Ctrl事件 during in %f seconds.", (routerCtrlSearchStartTime - routerCtrlSearchEndTime));
- 測(cè)試結(jié)果
使用路由時(shí)測(cè)試結(jié)果
- 第一次
[During]路由注冊(cè)事件 during in -0.051065 seconds.
[During]路由查詢handler事件 during in -0.000395 seconds.
[During]路由查詢Ctrl事件 during in -0.012069 seconds.
- 第二次
[During]路由注冊(cè)事件 during in -0.051075 seconds.
[During]路由查詢handler事件 during in -0.000451 seconds.
[During]路由查詢Ctrl事件 during in -0.009753 seconds.
- 第三次
[During]路由注冊(cè)事件 during in -0.050468 seconds.
[During]路由查詢handler事件 during in -0.000345 seconds.
[During]路由查詢Ctrl事件 during in -0.010102 seconds.
不使用路由時(shí)Push的測(cè)試結(jié)果
- 第一次
[During]普通push測(cè)試 during in -0.009095 seconds.
- 第二次
[During]普通push測(cè)試 during in -0.010562 seconds.
- 第三次
[During]普通push測(cè)試 during in -0.009745 seconds.
- 結(jié)論
- 路由注冊(cè)基本維持在50毫秒
- 路由查詢handler基本維持在3毫秒
- 不使用路由和使用路由時(shí)間基本相同
未來(lái)優(yōu)化
1.路由服務(wù)端配置傳入
2.當(dāng)Controller出現(xiàn)Bug時(shí)瞳遍,通過(guò)后臺(tái)配置對(duì)應(yīng)的type為MTRouterModelTypeH5闻妓,轉(zhuǎn)到H5頁(yè)面
3.json結(jié)構(gòu)如下
{
"module1": {
"firstCtrl": {
"identifier": "FirstViewController",
"type": "MTRouterModelTypeController",
},
"secondCtrl": {
"identifier": "http://www.meitu.com",
"type": "MTRouterModelTypeH5",
}
}
}