diff --git a/YBNetwork.podspec b/YBNetwork.podspec index 718de2f..273f081 100644 --- a/YBNetwork.podspec +++ b/YBNetwork.podspec @@ -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 网络中间层,注重性能,设计简洁,易于拓展。 diff --git a/YBNetwork/YBBaseRequest.h b/YBNetwork/YBBaseRequest.h index 5a70cde..2d38e61 100644 --- a/YBNetwork/YBBaseRequest.h +++ b/YBNetwork/YBBaseRequest.h @@ -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; diff --git a/YBNetwork/YBBaseRequest.m b/YBNetwork/YBBaseRequest.m index 0b03eb3..acfbfb7 100644 --- a/YBNetwork/YBBaseRequest.m +++ b/YBNetwork/YBBaseRequest.m @@ -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 { diff --git a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m index b2bc665..5a522ff 100644 --- a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m +++ b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m @@ -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 {