VVModularity
功能
- 模块化中间件
- 解耦,模块无需包含本中间件头文件
- 支持URL
- 异步操作,可设置超时,默认30秒超时
安装
VVModularity支持CocoaPods。请在Podfile中添加以下内容:
pod 'VVModularity'
用法
模块
+performTask:
和 +performAction:parameters:progress:success:failure:
二者只需实现其中之一;其中 +performTask:
需要包含 VVModularity.h
。
+cancelAction:
performTask方式可以在操作中定义task.cancel()达到同样目的;可选实现;如果不实现,当Action超时时,不会取消模块的操作,但是最终也不会处理操作结果。
+supportedActions:
可选实现;如果实现,则会先检查action是否被模块支持。
示例
+ (void)performAction:(NSString *)action
parameters:(nullable id)parameters
progress:(nullable NSProgress *)progress
success:(nullable void (^)(id __nullable responseObject))success
failure:(nullable void (^)(NSError *error))failure{
if([action isEqualToString:@"aa"]){
NSInteger total = 10;
__block NSUInteger i = 0;
[NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
i ++;
if (progress) {
progress.completedUnitCount = i * progress.totalUnitCount / total;
}
NSLog(@"VVModuleAA-->: %@", @(i));
if(i >= total){
!success ? : success(progress);
[timer invalidate];
}
}];
}
}
调用模块
需要包含VVModularity.h
头文件
VVModuleTask *task = [VVModuleTask taskWithTarget:@"ma" action:@"aa"];
task.progress = [NSProgress progressWithTotalUnitCount:100];
[task setSuccess:^(id responseObject) {
NSLog(@"response: %@", responseObject);
}];
[VVModularity performModuleTask:task];
//操作成功或失败无需处理
[VVModularity performTarget:@"VVModuleBB" action:@"bb" parameters:nil]
URL方式,请在AppDelegate中加入相应代码
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [VVModularity openURL:url completionHandler:nil];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
return [VVModularity openURL:url completionHandler:nil];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options{
return [VVModularity openURL:url completionHandler:nil];
}
作者
pozi119, [email protected]
协议
VVModularity 依照 MIT 协议进行许可。请查阅 LICENSE 文件获取更多信息。