Latest 0.0.1
Homepage https://github.com/xiongzenghuidegithub/XZHNtetwork
License MIT
Platforms ios 7.0, requires ARC
Dependencies AFNetworking, YYModel
Authors

Request网络库

首先非常感谢YTKNetwork贡献这么优质的代码,我也是一字一句看完了所有的代码实现,看完之后觉得有一些可以优化的地方,于是就斗胆发了出来。

YTKNetwork主要基于文件缓存,然后我将其做了一些优化:

  • (1) 主要依赖于内存缓存,在程序运行时,基本上只使用内存缓存
  • (2) 并提供内存缓存数据的存取多线程同步安全
  • (3) 内存缓存淘汰策略使用LRU算法
  • (4) 缓存查询,优先查询内存缓存,如果内存没有再去文件缓存读取
  • (5) App后台、退出、接收内存警告,才将内存缓存写入磁盘文件

不足之处:

  • (1) 下载暂未完成
  • (2) 上传暂未完成

因为也同样准备使用NSURLSession去实现。

主要的代码结构:

  • (1) 大部分实现都是基于YTKNetwork,但是YTKNetwork缓存都是使用文件缓存,将其改为主要依赖内存缓存,并提供了线程安全控制

  • (2) 最终是要AFNetworking,基于NSURLSesison实现版本,YTKNetwork仍然是基于NSOperation+NSURLConnection实现

  • (3) JSON转实体类集成了YYModel,YYModel中的缓存都是使用锁进行同步了的,所以是线程安全的

demo1、创建一个简单的请求

  • 继承自Request子类重写一个子类,并选择性重写如下所示方法实现
#import "KernelRequest.h"

@interface WeatherRequest : KernelRequest

- (instancetype)initWithkeyWord:(NSString *)keyWord;

@end
#import "WeatherRequest.h"
#import "WeatherResponse.h"

/**
 *  http://gc.ditu.aliyun.com/geocoding?a=苏州市
 */
@implementation WeatherRequest {
    NSString *_keyWord;
}

- (instancetype)initWithkeyWord:(NSString *)keyWord {
    if (self = [super init]) {
        _keyWord = [keyWord copy];
    }
    return self;
}

- (KernelRequestMethod)requestMethod {
    return KernelRequestMethodGET;
}

- (NSString *)host {
    return @"http://gc.ditu.aliyun.com";
}

- (NSString *)requestURL {
    return @"/geocoding";
}

- (id)requestArgument {
    return @{
             @"a" : _keyWord,
             };
}

- (KernelRequestAPIType)requestApiType {
    return KernelRequestAPITypeRestfull;
}

- (NSTimeInterval)cacheDurationTime {
    return 30;
}

- (long long)cacheVersion {
    return 1;
}

- (Class)responseClass {
    return [WeatherModel class];
}

- (KernelResponseThreadType)responseThread {
    return KernelResponseThreadTypeBackgroudThread;
}

@end
  • (2) 使用上面的Request类对象进行网络请求
WeatherRequest *req1 = [[WeatherRequest alloc] initWithkeyWord:@"苏州市"];

[req1 startWithSuccessComplet:^(KernelBaseRequest *request) {

    // 使用缓存
    KernelRequest *req = (KernelRequest *)request;
    id cacheJSON = [req cachedJSON];
    if (cacheJSON) {
        NSLog(@"cached response object = %@", cacheJSON);
    } else {
        NSLog(@"response object = %@", request.responseObject);
    }

    // json 解析
    WeatherModel *model = req1.responseModelObject;
    NSLog(@"model.address = %@", model.address);
    NSLog(@"model.alevel = %ld", model.alevel);
    NSLog(@"model.level = %ld", model.level);
    NSLog(@"model.cityName = %@", model.cityName);
    NSLog(@"model.lat = %@", model.lat);
    NSLog(@"model.lon = %@", model.lon);

} failComplet:^(KernelBaseRequest *request) {

    NSLog(@"error = %@", request.responseError);
}];

demo2、创建一个链式请求

- (void)test3 {

    //1. 创建一个链式请求
    KernelChainRequest *chainReq = [[KernelChainRequest alloc] init];

    //2. 创建一个基本请求
    WeatherRequest *req1 = [[WeatherRequest alloc] initWithkeyWord:@"苏州市"];

    // 3. 向链式中添加一个基本请求
    [chainReq addBasicRequest:req1 didBasicRequestFinished:^(KernelChainRequest * _Nonnull chainReq, KernelRequest * _Nonnull basicReq)
    {
        //4. 创建二个基本请求
        Wearther2Request *req2 = [[Wearther2Request alloc] initWithkeyWord:@"上海市"];

        //5. 向链式中添加二个基本请求
        [chainReq addBasicRequest:req2 didBasicRequestFinished:^(KernelChainRequest * _Nonnull chainReq, KernelRequest * _Nonnull basicReq)
        {
            //6. 创建三个基本请求
            IPReqyest *req3 = [[IPReqyest alloc] initWithIp:@"218.4.255.255"];

            //7. 向链式中添加二个基本请求
            [chainReq addBasicRequest:req3 didBasicRequestFinished:^(KernelChainRequest * _Nonnull chainReq, KernelRequest * _Nonnull basicReq)
            {
                NSLog(@"");
            }];
        }];
    }];

    [chainReq startWithDelegate:self];
}

- (void)kernelChainRequestDidSuccessFinish:(KernelChainRequest *__nullable)chainReq {
    NSLog(@"链式请求全部执行完毕");
}

- (void)kernelChainRequest:(KernelChainRequest *__nullable)chainReq didBasicRequestFailed:(KernelRequest *__nullable)request
{
    NSLog(@"其中有一个错误执行结束: %@", request);
}

demo3、创建一个批量请求

- (void)test4 {

    WeatherRequest *req1 = [[WeatherRequest alloc] initWithkeyWord:@"苏州市"];
    Wearther2Request *req2 = [[Wearther2Request alloc] initWithkeyWord:@"上海市"];
    IPReqyest *req3 = [[IPReqyest alloc] initWithIp:@"218.4.255.255"];
    NSArray *requests = @[req1, req2, req3];

    KernelBatchRequest *batchReq = [[KernelBatchRequest alloc] initWithBasicRequestArray:requests];
    [batchReq startWithDelegate:self];
}

- (void)batchRequestDidSuccessFinish:(KernelBatchRequest *)batchRequest {
    NSLog(@"批量请求全部执行完毕");
}

- (void)batchRequest:(KernelBatchRequest *)batchReq didBasicRequestFailed:(KernelRequest *)request {
    NSLog(@"其中有一个错误执行结束: %@", request);
}

demo4、网络层的日志打印

- (void)test5 {
    [KernelInterfaceAdapter sharedInstance].delegate = self;
}

/**
 *  KernelInterfaceAdapter日志信息回调
 *
 *  @param level   日志级别
 *  @param module  模块名称
 *  @param content 日志正文
 */
- (void)kernelInterfaceLogAdapterWithLevel:(KernelLogOutputLevel)level
                                    module:(NSString *__nonnull)module
                                   content:(NSString *__nullable)content
{
    //do your things about log infos.....
}

CocoaPods安装

pod 'XZHNetwork'

Latest podspec

{
    "name": "XZHNetwork",
    "version": "0.0.1",
    "summary": "XZHNetworku5927u90e8u5206u6e90u7801u53c2u8003u81eaYTKNetworkuff0cu53eau662fu505au4e86u4e00u4e9bu5c0fu90e8u5206u7684u4feeu6539uff0cu6bd4u5982u4f7fu7528NSURLSessionu3001u4ee5u53cau5927u91cfu4f7fu7528u5185u5b58u7f13u5b58u3002",
    "homepage": "https://github.com/xiongzenghuidegithub/XZHNtetwork",
    "license": "MIT",
    "authors": {
        "xiongzenghui": "[email protected]"
    },
    "source": {
        "git": "https://github.com/xiongzenghuidegithub/XZHNtetwork.git",
        "tag": "0.0.1"
    },
    "source_files": "XZHNtetwork/*.{h,m}",
    "platforms": {
        "ios": "7.0"
    },
    "requires_arc": true,
    "dependencies": {
        "AFNetworking": [],
        "YYModel": []
    }
}

Pin It on Pinterest

Share This