PS: 项目中使用了YTKNetWork来处理数据请求,TYK的特点就是把请求都按照request进行了封装,一个接口,一个request文件,截至到目前,已经存在近200个Request类,要疯的节奏;一方面类多了对程序也是个负担,另外开发过程中看着也难受。
因此给YTKBaseRequest封装了一个壳子,壳子写法分为一下两种
思路差不多,使用上也大同小异,先把使用方法贴一下,大家选着看一下。代码在gitee上ShellRequestForYTK
- 第一种
SShellRequest * ssRequest = [[SShellRequest alloc]init];
ssRequest.requestUrlBlock(@"/mock/route/to/shell")
.requestArgumentBlock(@{})
.requestMethodBlock(YTKRequestMethodGET)
.shellSuccessblock(^(__kindof ShellAPIResponse * _Nullable response) {
NSLog(@"SShellRequest~~~~~~~~~%@, %@", response.returnCode, response.returnData);
})
.shellFailureblock(^(__kindof ShellAPIResponse * _Nullable response) {
NSLog(@"SShellRequestXXXXXXXX%@", response.returnCode);
})
.startBlock();
- 第二种
ShellRequest * request = [[ShellRequest alloc] init];
request.shellSuccessblock(^(__kindof ShellAPIResponse * _Nullable response) {
NSLog(@"~~~~~~~~~%@, %@", response.returnCode, response.returnMessage);
})
.shellFailureblock(^(__kindof ShellAPIResponse * _Nullable response) {
NSLog(@"XXXXXXXXXXXXXXX%@", response.returnCode);
})
.requestUrlBlock(^NSString * _Nullable{
return @"/mock/route/to/shell";
})
.requestArgumentBlock(^id _Nullable{
return (id)@{};
})
.requestMethodBlock(^YTKRequestMethod{
return YTKRequestMethodGET;
});
request.startBlock();
第一种:壳子直接缓存属性
,壳子里根据YTK重写的一些方法,缓存了属性,使YTK调用的时候可以找到对应的属性。写法上参考了Masonry,尽量使设置属性的方法集中。
先贴使用方法
写法也是简单粗暴,
@property(nonatomic, copy, readonly)SShellRequest *_Nullable(^requestUrlBlock)(NSString *);
.m中,,
// 声明缓存
NSString * _kRequestUrl;
// 重写YTK方法
- (NSString *)requestUrl {
return _kRequestUrl ?: @"";
}
// 设置属性方法
- (SShellRequest * _Nullable (^)(NSString * _Nonnull))requestUrlBlock {
__weak typeof(self) weakSelf = self;
return ^SShellRequest * _Nullable(NSString * _Nonnull url) {
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf->_kRequestUrl = url;
return weakSelf;
};
}
// 调用start
- (ShellStartBlock)startBlock {
__weak typeof(self) weakSelf = self;
return ^ShellRequest * _Nullable{
[weakSelf startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
ShellAPIResponse* response = [[ShellAPIResponse alloc] init];
weakSelf.successResponseBlock(response);
} failure:^(__kindof YTKBaseRequest * _Nonnull request) {
ShellAPIResponse* response = [[ShellAPIResponse alloc] init];
weakSelf.failureResponseBlock(response);
}];
return weakSelf;
};
}
第二种:壳子缓存了Block
,壳子里根据YTK重写的一些方法,缓存了block,使YTK调用的时候可以找到对应的属性。写法上参考了Masonry,尽量使设置属性的方法集中。
思路是,创建属性设置的block, 然后缓存起来,“属性设置的block” 也用一个block作为属性进行设置,这样就可以使用点语法。
关键代码:
//ShellAPIResponse 里面包裹了返回值,状态,和data
typedef void(^ABCAPIResponseBlock)(__kindof ShellAPIResponse * _Nullable response);
typedef ShellRequest *_Nullable(^ShellStartBlock)(void);
typedef ShellRequest *_Nullable(^ShellRequestCompletionBlock)(ABCAPIResponseBlock);
@interface ShellRequest : YTKRequest
//MARK: 用户可供调用的设置url等的block
@property(nonatomic, copy, readonly)ShellRequest *_Nullable(^requestUrlBlock)(FinalRequestUrlBlock);
@property(nonatomic, copy, readonly)ShellRequestCompletionBlock shellSuccessblock;
@property(nonatomic, copy, readonly)ShellRequestCompletionBlock shellFailureblock;
/// 开始请求的block,直接使用,并保证最后使用
@property(nonatomic, copy, readonly)ShellStartBlock startBlock;
@end
NS_ASSUME_NONNULL_END
ShellRequest.m 中关键代码如下:
@interface ShellRequest ()
{
FinalRequestUrlBlock _kRequestUrlBlock;
}
@end
@implementation ShellRequest
- (NSString *)requestUrl{
return _kRequestUrlBlock ? _kRequestUrlBlock() : @"";
}
// 调用start
- (ShellStartBlock)startBlock {
__weak typeof(self) weakSelf = self;
return ^ShellRequest * _Nullable{
[weakSelf startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
ShellAPIResponse* response = [[ShellAPIResponse alloc] init];
weakSelf.failureResponseBlock(response);
weakSelf.successResponseBlock(response);
} failure:^(__kindof YTKBaseRequest * _Nonnull request) {
NSInteger statusCode = request.responseStatusCode;
ShellAPIResponse* response = [[ShellAPIResponse alloc] init];
weakSelf.failureResponseBlock(response);
}];
return weakSelf;
};
}
// 设置缓存block们
- (ShellRequest * _Nullable (^)(FinalRequestUrlBlock _Nonnull))requestUrlBlock {
__weak typeof(self) weakSelf = self;
return ^ShellRequest * _Nullable(FinalRequestUrlBlock urlBlock) {
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf->_kRequestUrlBlock = urlBlock;
return weakSelf;
};
}
@end
网友评论