Skip to content

Commit

Permalink
[feature] 添加网络请求回调重定向入口
Browse files Browse the repository at this point in the history
  • Loading branch information
indulgeIn committed Sep 9, 2019
1 parent 433799a commit f7ae4e4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion YBNetwork.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 网络中间层,注重性能,设计简洁,易于拓展。
Expand Down
4 changes: 4 additions & 0 deletions YBNetwork/Response/YBNetworkResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

NS_ASSUME_NONNULL_BEGIN

/**
网络请求响应对象
如果想拓展一些属性,使用 runtime 关联属性,然后重写预处理方法进行计算并赋值就行了。
*/
@interface YBNetworkResponse : NSObject

/// 请求成功数据
Expand Down
12 changes: 10 additions & 2 deletions YBNetwork/YBBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ NS_ASSUME_NONNULL_BEGIN
/** 请求标识,可以查看完整的请求路径和参数 */
- (NSString *)requestIdentifier;

/** 清空所有请求回调闭包 */
- (void)clearRequestBlocks;

#pragma - 网络请求公共配置 (以子类化方式实现: 针对不同的接口团队设计不同的公共配置)

/**
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 26 additions & 16 deletions YBNetwork/YBBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
})
}

Expand Down Expand Up @@ -242,6 +258,7 @@ - (void)successWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)c
if (self.successBlock) {
self.successBlock(response);
}
[self clearRequestBlocks];
}
})
}
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions YBNetwork/YBNetworkDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ typedef NS_ENUM(NSInteger, YBNetworkRepeatStrategy) {
YBNetworkRepeatStrategyCancelNewest
};

/// 网络请求回调重定向类型
typedef NS_ENUM(NSInteger, YBRequestRedirection) {
/// 重定向为成功
YBRequestRedirectionSuccess,
/// 重定向为失败
YBRequestRedirectionFailure,
/// 停止后续操作(主要是停止回调)
YBRequestRedirectionStop
};


@class YBBaseRequest;
@class YBNetworkResponse;
Expand Down

0 comments on commit f7ae4e4

Please sign in to comment.