iOS网络层业务层-dianping api 更改delegate方式为block回调

1.使用代理方式发送请求的弊端

#import "TESTViewController.h"
#import "DPAPI.h"

@interface TESTViewController () <DPRequestDelegate>
@property (nonatomic,strong) DPAPI *api;
@property (nonatomic,strong) DPRequest *request1;
@property (nonatomic,strong) DPRequest *request2;
@property (nonatomic,strong) DPRequest *request3;
@end

@implementation TESTViewController

- (DPAPI *)api{
    if (_api==nil) {
        _api = [[DPAPI alloc] init];
    }
    return _api;
}

- (void)request:(DPRequest *)request didReceiveResponse:(NSURLResponse *)response{
    
}

- (void)request:(DPRequest *)request didReceiveRawData:(NSData *)data{
    
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
    //NSLog(@"result = %@",result);
    if (request == _request1) {
        
    }
    else if (request == _request2){
        
    }
    else if (request == _request3){
        
    }
    else{
        
    }
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
    //NSLog(@"error  = %@",error);
    if (request == _request1) {
        
    }
    else if (request == _request2){
        
    }
    else if (request == _request3){
        
    }
    else{
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    dict1[@"city"] = @"北京";
    _request1 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict1 delegate:self];
    
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    dict2[@"city"] = @"上海";
    _request2 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict2 delegate:self];
    
    NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
    dict3[@"city"] = @"深圳";
    _request3 = [self.api requestWithURL:@"v1/deal/find_deals" params:dict3 delegate:self];
}
@end

整个项目到处都在发送请求 ,需要发送大量的请求,需要对发送请求进行封装

2.给DPRequest添加2个block属性,封装block形式的接口

点评接口是整个项目到处都需要使用的,团购展示页面、团购详情页面…都需要使用SDK的接口,整个项目都需要发送请求、所以发送请求的代码不能封装在控制器中,要封装到工具类

就不叫httpTool了,因为我们不管大众点评的API发送的是什么请求
我调用大众点评的东西,它里面发什么请求我不知道

工具类你去发一个请求,请求地址参数 告诉你,请求成功了告诉我 ,请求失败了告诉我

Request参数 和 params参数是传给接口的
Json和error参数是接口传出来的

1-DPRequestDelegate.png

协议方法都是对象方法,说明对象才能作为代理

以前写工具类使用类方法,这次特殊因为工具类要实现<DPRequestDelegate>协议所以要用到工具类的对象,所以该接口设计为对象方法

别人拿到我的ZYXAPITool ,给我请求urlString 和 参数 paramsDict ,success blockfailure block
ZYXAPITool拿到DPAPI对象,发送请求,请求成功或失败调用ZYXAPITool对象实现的<DPRequestDelegate>协议中的代理方法

但是DPAPI对象只需要创建一次,在ZYXAPITool使用一个strong引用它即可

2-DPRequestDelegate请求成功和失败的代理方法.png

请求成功和失败的两个代理方法
是拿不到接口传入的success() 和 failure() 这两个block 参数的
并且如果连续调用4次该ZYXAPITool接口方法,会有4个success 和 4个failure block参数

ZYXAPITool设计的接口方法和两个DPRequestDelegate代理方法的桥梁?

3-桥梁就是DPRequest对象.png

可以把success 和 failure 两个参数block 塞给

4-dpapi接口方法返回的DPRequest对象.png

然后把 DPRequest *request对象 传给 DPRequestDelegate协议的两个代理方法

5-DPRequest增加2个block类型的成员变量.png

DPRequest.h

#warning 增加2个请求的block
@property (nonatomic, copy) void (^success)(id jsonObj);
@property (nonatomic, copy) void (^failure)(NSError *error);

ZYXAPITool.h

#import <Foundation/Foundation.h>
@interface ZYXAPITool : NSObject

- (void)request:(NSString *)urlString
         params:(NSDictionary *)paramsDict
        success:(void (^)(id jsonObj))success
        failure:(void (^)(NSError *error))failure;
@end

ZYXAPITool.m

#import "ZYXAPITool.h"
#import "DPAPI.h"

@interface ZYXAPITool() <DPRequestDelegate>
@property (nonatomic, strong) DPAPI *api;
@end

@implementation ZYXAPITool

- (DPAPI *)api{
    if (_api == nil) {
        self.api = [[DPAPI alloc] init];
    }
    return _api;
}

- (void)request:(NSString *)urlString
         params:(NSDictionary *)paramsDict
        success:(void (^)(id jsonObj))success
        failure:(void (^)(NSError *))failure{
    DPRequest *request = [self.api requestWithURL:urlString
                                           params:[NSMutableDictionary dictionaryWithDictionary:paramsDict]
                                         delegate:self];
    request.success = success;
    request.failure = failure;
}

#pragma mark -  DPRequestDelegate
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
    if (request.success) {
        request.success(result);
    }
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
    if (request.failure) {
        request.failure(error);
    }
}

@end

因为ZYXAPITool要实现<DPRequestDelegate>协议所以设计为对象接口

使用ZYXAPITool发送请求,DPAPI返回一个DPRequest对象,通过返回的DPRequest对象把传入的success 和 failure 两个block参数 存起来

请求大众点评的服务器成功 把传入的success调用一下
失败同理

block类型的成员使用copy关键字

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"city"] = @"北京";
    
    ZYXAPITool *apiTool = [[ZYXAPITool alloc] init];
    [apiTool request:@"v1/deal/find_deals"
              params:dict
             success:^(id jsonObj) {
                NSLog(@"success jsonObj = %@",jsonObj);
             }
             failure:^(NSError *error) {
                NSLog(@"failure error   = %@",error);
             }];
2016-08-25 19:20:30.124 大众点评API[60093:2307979] success jsonObj = {
count = 20;
deals = (
            {
        businesses =             (
                            {
                city = "\U5317\U4eac";
                "h5_url" = "http://m.dianping.com/shop/65685091?utm_source=open";
                id = 65685091;
                latitude = "39.89661";
                longitude = "116.41923";
                name = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce";
                url = "http://www.dianping.com/shop/65685091?utm_source=open";
            }
        );
        categories =             (
            "\U684c\U6e38/\U7535\U73a9/\U7f51\U5427"
        );
        city = "\U5317\U4eac";
        "commission_ratio" = 0;
        "current_price" = 40;
        "deal_h5_url" = "http://m.dianping.com/tuan/deal/20401897?utm_source=open";
        "deal_id" = "2-20401897";
        "deal_url" = "http://t.dianping.com/deal/20401897?utm_source=open";
        description = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce \U4ec5\U552e40\U5143\Uff0c\U4ef7\U503c50\U5143\U6e38\U620f\U5e0150\U4e2a\Uff01";
        distance = "-1";
        "image_url" = "http://i1.s2.dpfile.com/pc/mc/625e128afdd3b62caf71fc228d22c2c8(640c400)/thumb.jpg";
        "list_price" = 50;
        "publish_date" = "2016-06-24";
        "purchase_count" = 412;
        "purchase_deadline" = "2017-06-30";
        regions =             (
            "\U4e1c\U57ce\U533a"
        );
        "s_image_url" = "http://i1.s2.dpfile.com/pc/mc/625e128afdd3b62caf71fc228d22c2c8(160c100)/thumb.jpg";
        title = "\U6469\U6839\U5927\U53e4\U7535\U73a9\U57ce";
    },

其余的19组数据 

    );
    status = OK;
    "total_count" = 114644;
}

3.ZYXAPITool的单例设计模式

控制器需要发送请求,都需要实例化 ZYXAPITool 没必要全局创建多个全局拥有一个api对象,就可以发送所有的请求了

ZYXSingleton.h

// .h文件
#define ZYXSingletonH(name) + (instancetype)shared##name;

// .m文件
#define ZYXSingletonM(name) \
static id _instance = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
+ (instancetype)shared##name \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instance; \
}

ZYXAPITool.h

#import <Foundation/Foundation.h>
#import "ZYXSingleton.h"

@interface ZYXAPITool : NSObject

- (void)request:(NSString *)urlString
         params:(NSDictionary *)paramsDict
        success:(void (^)(id jsonObj))success
        failure:(void (^)(NSError *error))failure;

ZYXSingletonH(APITool)

@end

ZYXAPITool.m

#import "ZYXAPITool.h"
#import "DPAPI.h"

@interface ZYXAPITool() <DPRequestDelegate>
@property (nonatomic, strong) DPAPI *api;
@end

@implementation ZYXAPITool

ZYXSingletonM(APITool)

- (DPAPI *)api{
    if (_api == nil) {
        self.api = [[DPAPI alloc] init];
    }
    return _api;
}

- (void)request:(NSString *)urlString
         params:(NSDictionary *)paramsDict
        success:(void (^)(id jsonObj))success
        failure:(void (^)(NSError *))failure{
    DPRequest *request = [self.api requestWithURL:urlString
                                           params:[NSMutableDictionary dictionaryWithDictionary:paramsDict]
                                         delegate:self];
    request.success = success;
    request.failure = failure;
}

#pragma mark -  DPRequestDelegate
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
    if (request.success) {
        request.success(result);
    }
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
    if (request.failure) {
        request.failure(error);
    }
}

@end

4.网络工具类 和 具体业务类 理解

6-项目分层.png
7-ZYXAPITool返回jsonObj字典.png

因为ZYXAPITool是一个通用的工具类,肯定是返回字典对象,任何人都可以使用它发请求,请求成功返回的是jsonObj字典对象,不是模型对象

如果ZYXAPITool返回模型对象就不是通用的了,发任何请求都返回一个特定的模型对象,那么这个工具类就只能做一件事情了,作为APITool是应该能发任何请求的,所以返回什么APITool不用管,你把jsonObj字典对象返回给我就好了

业务层才是具体的,要返回模型对象,把APITool返回的jsonObj字典对象解析成模型对象

作为通用的工具类应该用通用的数据类型,参数是字典对象 返回值也是字典对象

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,142评论 1 23
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,798评论 6 13
  • AFHTTPRequestOperationManager 网络传输协议UDP、TCP、Http、Socket、X...
    Carden阅读 4,306评论 0 12
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,490评论 18 139
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,107评论 29 470