网络框架

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

- (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 了.

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

/**
 *  网络请求 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 的扩展:

#import "UIViewController+NetworkOperationManager.h"
#import 
#import "NetworkRequestManager.h"

static NSString *kOperationKey = @"kOperationKey";

@implementation UIViewController     (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 就能很好的管理我们的网络请求了

@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

你可能感兴趣的:(网络框架)