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 f7ae4e4 commit b6ce1ee
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 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.1"
s.version = "1.0.3"
s.summary = "基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展"
s.description = <<-DESC
基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展。
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/YBBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ NS_ASSUME_NONNULL_BEGIN
@interface YBBaseRequest (PreprocessResponse)

/**
网络请求回调重定向,将会再下面几个预处理方法之前调用
网络请求回调重定向,方法在子线程回调,并会再下面几个预处理方法之前调用
需要特别注意 YBRequestRedirectionStop 会停止后续操作,如果业务使用闭包回调,这个闭包不会被清空,可能会造成循环引用,所以这种场景务必保证回调被正确处理,一般有以下两种方式:
1、Stop 过后执行特定逻辑,然后重新 start 发起网络请求,之前的回调闭包就能继续正常处理了。
2、直接调用 clearRequestBlocks 清空回调闭包。
*/
- (YBRequestRedirection)yb_redirectionWithResponse:(YBNetworkResponse *)response;
- (void)yb_redirection:(void(^)(YBRequestRedirection))redirection response:(YBNetworkResponse *)response;

/** 预处理请求成功数据 (子线程执行, 若数据来自缓存在主线程执行) */
- (void)yb_preprocessSuccessInChildThreadWithResponse:(YBNetworkResponse *)response;
Expand Down
44 changes: 23 additions & 21 deletions YBNetwork/YBBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,29 +202,31 @@ - (void)requestDownloadProgress:(NSProgress *)progress {
}

- (void)requestCompletionWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)cacheKey fromCache:(BOOL)fromCache taskID:(NSNumber *)taskID {
YBRequestRedirection redirection;
if ([self respondsToSelector:@selector(yb_redirectionWithResponse:)]) {
redirection = [self yb_redirectionWithResponse:response];
} else {
redirection = response.error ? YBRequestRedirectionFailure : YBRequestRedirectionSuccess;
}

switch (redirection) {
case YBRequestRedirectionSuccess: {
[self successWithResponse:response cacheKey:cacheKey fromCache:NO];
}
break;
case YBRequestRedirectionFailure: {
[self failureWithResponse:response];
void(^process)(YBRequestRedirection) = ^(YBRequestRedirection redirection) {
switch (redirection) {
case YBRequestRedirectionSuccess: {
[self successWithResponse:response cacheKey:cacheKey fromCache:NO];
}
break;
case YBRequestRedirectionFailure: {
[self failureWithResponse:response];
}
break;
case YBRequestRedirectionStop:
default: break;
}
break;
case YBRequestRedirectionStop:
default: break;
}

YBNETWORK_MAIN_QUEUE_ASYNC(^{
[self.taskIDRecord removeObject:taskID];
})
};

YBNETWORK_MAIN_QUEUE_ASYNC(^{
[self.taskIDRecord removeObject:taskID];
})
if ([self respondsToSelector:@selector(yb_redirection:response:)]) {
[self yb_redirection:process response:response];
} else {
YBRequestRedirection redirection = response.error ? YBRequestRedirectionFailure : YBRequestRedirectionSuccess;
process(redirection);
}
}

- (void)successWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)cacheKey fromCache:(BOOL)fromCache {
Expand Down
7 changes: 4 additions & 3 deletions YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ - (void)start {
[super start];
}

- (BOOL)yb_preprocessShouldFailedWithResponse:(YBNetworkResponse *)response {
- (void)yb_redirection:(void (^)(YBRequestRedirection))redirection response:(YBNetworkResponse *)response {
NSDictionary *responseDic = response.responseObject;
if ([responseDic isKindOfClass:NSDictionary.self] && [[NSString stringWithFormat:@"%@", responseDic[@"error_code"]] isEqualToString:@"2"]) {
redirection(YBRequestRedirectionFailure);
response.errorType = YBResponseErrorTypeServerError;
return YES;
return;
}
return NO;
redirection(YBRequestRedirectionSuccess);
}

- (NSDictionary *)yb_preprocessParameter:(NSDictionary *)parameter {
Expand Down

0 comments on commit b6ce1ee

Please sign in to comment.