码迷,mamicode.com
首页 > 其他好文 > 详细

网络框架

时间:2019-10-18 17:29:19      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:seo   end   operation   nslog   直接   void   uil   有一个   ams   

最近项目上准备把引入的Mantle替换为YYModel, 之前也听别人书把model传到网络模块中,这样返回的就是转换好的 entity了, 于是今天做了一下实现.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (id)POST:(NSString *)address params:(NSDictionary *)params targetModelClass:(Class)targetModel completionHnadle:(CompletionHandle)handle{
id requestOperation;
requestOperation = [self requestWithMethod:@"POST" address:address params:params completionHnadle:^(id operation, id responseObject, NSError *error) {
if (error)
{
handle(operation, nil, error);
}
else
{
if (targetModel)
{
id model = [targetModel yy_modelWithDictionary:responseObject];
handle (operation, model , nil);
}
else
{
handle (operation, responseObject, nil);
}
}
}];
return requestOperation;
}

这样拿到了 targetModel就可以直接转换为对应的 entity 了.

顺便还写了一个管理网络请求的队列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
* 网络请求 operation 管理类
*
* @return 管理者
*/
+ (instancetype)shareManager;
* 添加一个网络请求操作到数组中
*
* @param operation void
*/
- (void)addOperation:(id)operation;
* 移除一个网络请求对象
*
* @param operation void
*/
- (void)removeOperation:(id)operation;
* 移除所有的网络请求
*/
- (void)removeAllOperation;
* 获取所有的网络请求数组
*
* @return 网络请求的数组
*/
- (NSArray *)allOperation;

还有一个需求 , 就是有的 app 要求退出当前页面的时候,要取消当前页面所有没有完成的网络请求, 于是又写了一个基于 UIViewController 的扩展:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24< 大专栏  网络框架/div>
25
26
27
28
29
30
31
32
33
34
35
#import <objc/objc-runtime.h>
#import "NetworkRequestManager.h"
static NSString *kOperationKey = @"kOperationKey";
@implementation (NetworkOperationManager)
-(NSMutableArray *)operations
{
id obj = objc_getAssociatedObject(self, &kOperationKey);
if (!obj) {
obj = [[NSMutableArray alloc]init];
objc_setAssociatedObject(self, &kOperationKey, obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return obj;
}
-(void)addOperation:(id)operation
{
[self.operations addObject:operation];
}
-(void)cancleOperation:(id)operation
{
[self.operations removeObject:operation];
}
-(void)cancelAllOperation
{
[[NetworkRequestManager shareManager] removeAllRequestOperations];
[self.operations removeAllObjects];
}
@end

这样我们在 controller 就能很好的管理我们的网络请求了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLSessionDataTask *op = [[NetworkRequestManager shareManager] GET:@"http://114.80.110.197/v3/028/json/reply/GetRegionPostsRequest" params:nil targetModelClass:[TestModel class] completionHnadle:^(id operation, id targetData, NSError *error) {
TestModel *model = (TestModel *)targetData;
[model.result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
TestModel2 *model2 = (TestModel2 *)obj;
NSLog(@"测试地址 : %@", model2.address);
}];
}];
[self addOperation:op];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
// 退出界面时 取消所有未完成的网络请求
[self cancelAllOperation];
}
@end

项目已上传至 github NetWork简单封装的一套网络请求框架, 支持 NSURLSession / NSURLConnection

网络框架

标签:seo   end   operation   nslog   直接   void   uil   有一个   ams   

原文地址:https://www.cnblogs.com/dajunjun/p/11699434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!