From f7ae4e42e78f95627479e4d19bb610d43b51cd12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B3=A2?= <1106355439@qq.com> Date: Mon, 9 Sep 2019 13:51:05 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20=E6=B7=BB=E5=8A=A0=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E8=AF=B7=E6=B1=82=E5=9B=9E=E8=B0=83=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YBNetwork.podspec | 2 +- YBNetwork/Response/YBNetworkResponse.h | 4 +++ YBNetwork/YBBaseRequest.h | 12 ++++++-- YBNetwork/YBBaseRequest.m | 42 ++++++++++++++++---------- YBNetwork/YBNetworkDefine.h | 10 ++++++ 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/YBNetwork.podspec b/YBNetwork.podspec index 68263bf..718de2f 100644 --- a/YBNetwork.podspec +++ b/YBNetwork.podspec @@ -4,7 +4,7 @@ Pod::Spec.new do |s| s.name = "YBNetwork" - s.version = "1.0" + s.version = "1.0.1" s.summary = "基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展" s.description = <<-DESC 基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展。 diff --git a/YBNetwork/Response/YBNetworkResponse.h b/YBNetwork/Response/YBNetworkResponse.h index 92f171c..65465b3 100644 --- a/YBNetwork/Response/YBNetworkResponse.h +++ b/YBNetwork/Response/YBNetworkResponse.h @@ -11,6 +11,10 @@ NS_ASSUME_NONNULL_BEGIN +/** + 网络请求响应对象 + 如果想拓展一些属性,使用 runtime 关联属性,然后重写预处理方法进行计算并赋值就行了。 + */ @interface YBNetworkResponse : NSObject /// 请求成功数据 diff --git a/YBNetwork/YBBaseRequest.h b/YBNetwork/YBBaseRequest.h index 09b10a4..5a70cde 100644 --- a/YBNetwork/YBBaseRequest.h +++ b/YBNetwork/YBBaseRequest.h @@ -80,6 +80,9 @@ NS_ASSUME_NONNULL_BEGIN /** 请求标识,可以查看完整的请求路径和参数 */ - (NSString *)requestIdentifier; +/** 清空所有请求回调闭包 */ +- (void)clearRequestBlocks; + #pragma - 网络请求公共配置 (以子类化方式实现: 针对不同的接口团队设计不同的公共配置) /** @@ -116,8 +119,13 @@ NS_ASSUME_NONNULL_BEGIN /// 预处理响应数据 (重写分类方法) @interface YBBaseRequest (PreprocessResponse) -/** 是否将响应成功转换为响应错误(在某些情况下服务器返回一些代表访问错误的状态码,就可以重写这个方法直接转换为错误响应) */ -- (BOOL)yb_preprocessShouldFailedWithResponse:(YBNetworkResponse *)response; +/** + 网络请求回调重定向,将会再下面几个预处理方法之前调用。 + 需要特别注意 YBRequestRedirectionStop 会停止后续操作,如果业务使用闭包回调,这个闭包不会被清空,可能会造成循环引用,所以这种场景务必保证回调被正确处理,一般有以下两种方式: + 1、Stop 过后执行特定逻辑,然后重新 start 发起网络请求,之前的回调闭包就能继续正常处理了。 + 2、直接调用 clearRequestBlocks 清空回调闭包。 + */ +- (YBRequestRedirection)yb_redirectionWithResponse:(YBNetworkResponse *)response; /** 预处理请求成功数据 (子线程执行, 若数据来自缓存在主线程执行) */ - (void)yb_preprocessSuccessInChildThreadWithResponse:(YBNetworkResponse *)response; diff --git a/YBNetwork/YBBaseRequest.m b/YBNetwork/YBBaseRequest.m index 0d07dbd..0b03eb3 100644 --- a/YBNetwork/YBBaseRequest.m +++ b/YBNetwork/YBBaseRequest.m @@ -125,6 +125,14 @@ - (BOOL)isExecuting { return isExecuting; } +- (void)clearRequestBlocks { + self.uploadProgress = nil; + self.downloadProgress = nil; + self.cacheBlock = nil; + self.successBlock = nil; + self.failureBlock = nil; +} + #pragma mark - request - (void)startWithCacheKey:(NSString *)cacheKey { @@ -194,20 +202,28 @@ - (void)requestDownloadProgress:(NSProgress *)progress { } - (void)requestCompletionWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)cacheKey fromCache:(BOOL)fromCache taskID:(NSNumber *)taskID { - BOOL shouldFailed = nil != response.error; - if (!shouldFailed && [self respondsToSelector:@selector(yb_preprocessShouldFailedWithResponse:)]) { - shouldFailed = [self yb_preprocessShouldFailedWithResponse:response]; + YBRequestRedirection redirection; + if ([self respondsToSelector:@selector(yb_redirectionWithResponse:)]) { + redirection = [self yb_redirectionWithResponse:response]; + } else { + redirection = response.error ? YBRequestRedirectionFailure : YBRequestRedirectionSuccess; } - if (shouldFailed) { - [self failureWithResponse:response]; - } else { - [self successWithResponse:response cacheKey:cacheKey fromCache:NO]; + switch (redirection) { + case YBRequestRedirectionSuccess: { + [self successWithResponse:response cacheKey:cacheKey fromCache:NO]; + } + break; + case YBRequestRedirectionFailure: { + [self failureWithResponse:response]; + } + break; + case YBRequestRedirectionStop: + default: break; } YBNETWORK_MAIN_QUEUE_ASYNC(^{ [self.taskIDRecord removeObject:taskID]; - [self clearRequestBlocks]; }) } @@ -242,6 +258,7 @@ - (void)successWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)c if (self.successBlock) { self.successBlock(response); } + [self clearRequestBlocks]; } }) } @@ -262,19 +279,12 @@ - (void)failureWithResponse:(YBNetworkResponse *)response { if (self.failureBlock) { self.failureBlock(response); } + [self clearRequestBlocks]; }) } #pragma mark - private -- (void)clearRequestBlocks { - self.uploadProgress = nil; - self.downloadProgress = nil; - self.cacheBlock = nil; - self.successBlock = nil; - self.failureBlock = nil; -} - - (NSString *)requestIdentifier { NSString *identifier = [NSString stringWithFormat:@"%@-%@%@", [self requestMethodString], [self validRequestURLString], [self stringFromParameter:[self validRequestParameter]]]; return identifier; diff --git a/YBNetwork/YBNetworkDefine.h b/YBNetwork/YBNetworkDefine.h index 919c908..30188da 100644 --- a/YBNetwork/YBNetworkDefine.h +++ b/YBNetwork/YBNetworkDefine.h @@ -93,6 +93,16 @@ typedef NS_ENUM(NSInteger, YBNetworkRepeatStrategy) { YBNetworkRepeatStrategyCancelNewest }; +/// 网络请求回调重定向类型 +typedef NS_ENUM(NSInteger, YBRequestRedirection) { + /// 重定向为成功 + YBRequestRedirectionSuccess, + /// 重定向为失败 + YBRequestRedirectionFailure, + /// 停止后续操作(主要是停止回调) + YBRequestRedirectionStop +}; + @class YBBaseRequest; @class YBNetworkResponse;