Skip to content

Commit

Permalink
[fix] 优化重定向内部逻辑,更改部分实现代码
Browse files Browse the repository at this point in the history
  • Loading branch information
indulgeIn committed Sep 27, 2019
1 parent b8fdecc commit 7dd3ce0
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 45 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.4"
s.version = "1.0.5"
s.summary = "基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展"
s.description = <<-DESC
基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展。
Expand Down
3 changes: 2 additions & 1 deletion YBNetwork/Manger/YBNetworkManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ - (AFHTTPSessionManager *)sessionManagerForRequest:(YBBaseRequest *)request {
manager = defaultManager;
}
manager.completionQueue = dispatch_queue_create("com.ybnetwork.completionqueue", DISPATCH_QUEUE_CONCURRENT);
if (request.responseSerializer) manager.responseSerializer = request.responseSerializer;
AFHTTPResponseSerializer *customSerializer = request.responseSerializer;
if (customSerializer) manager.responseSerializer = customSerializer;
return manager;
}

Expand Down
6 changes: 1 addition & 5 deletions YBNetwork/Response/YBNetworkResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ NS_ASSUME_NONNULL_BEGIN
/// 请求成功数据
@property (nonatomic, strong, nullable) id responseObject;

/// 请求失败类型 (使用该属性做业务处理足够)
@property (nonatomic, assign) YBResponseErrorType errorType;

/// 请求失败 NSError
@property (nonatomic, strong, readonly, nullable) NSError *error;


/// 请求任务
@property (nonatomic, strong, readonly, nullable) NSURLSessionTask *sessionTask;

/// sessionTask.response
@property (nonatomic, strong, readonly, nullable) NSHTTPURLResponse *URLResponse;


/// 便利构造
+ (instancetype)responseWithSessionTask:(nullable NSURLSessionTask *)sessionTask
responseObject:(nullable id)responseObject
error:(nullable NSError *)error;
Expand Down
17 changes: 1 addition & 16 deletions YBNetwork/Response/YBNetworkResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,7 @@ + (instancetype)responseWithSessionTask:(NSURLSessionTask *)sessionTask response
YBNetworkResponse *response = [YBNetworkResponse new];
response->_sessionTask = sessionTask;
response->_responseObject = responseObject;
if (error) {
response->_error = error;
YBResponseErrorType errorType;
switch (error.code) {
case NSURLErrorTimedOut:
errorType = YBResponseErrorTypeTimedOut;
break;
case NSURLErrorCancelled:
errorType = YBResponseErrorTypeCancelled;
break;
default:
errorType = YBResponseErrorTypeNoNetwork;
break;
}
response->_errorType = errorType;
}
response->_error = error;
return response;
}

Expand Down
14 changes: 0 additions & 14 deletions YBNetwork/YBNetworkDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ typedef NS_ENUM(NSInteger, YBRequestMethod) {
YBRequestMethodPATCH
};

/// 网络响应错误类型
typedef NS_ENUM(NSInteger, YBResponseErrorType) {
/// 未知
YBResponseErrorTypeUnknown,
/// 超时
YBResponseErrorTypeTimedOut,
/// 取消
YBResponseErrorTypeCancelled,
/// 无网络
YBResponseErrorTypeNoNetwork,
/// 服务器错误
YBResponseErrorTypeServerError
};

/// 缓存存储模式
typedef NS_OPTIONS(NSUInteger, YBNetworkCacheWriteMode) {
/// 无缓存
Expand Down
14 changes: 14 additions & 0 deletions YBNetworkDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2358A60C233DBB6D00623E4E /* YBNetworkResponse+YBCustom.m in Sources */ = {isa = PBXBuildFile; fileRef = 2358A60B233DBB6D00623E4E /* YBNetworkResponse+YBCustom.m */; };
23730AE9227FDFCA0033BB11 /* DefaultServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 23730AE5227FDFCA0033BB11 /* DefaultServerRequest.m */; };
23730AEA227FDFCA0033BB11 /* OtherServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 23730AE8227FDFCA0033BB11 /* OtherServerRequest.m */; };
23730AED227FE0400033BB11 /* TestViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 23730AEB227FE0400033BB11 /* TestViewController.m */; };
Expand All @@ -24,6 +25,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2358A60A233DBB6D00623E4E /* YBNetworkResponse+YBCustom.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "YBNetworkResponse+YBCustom.h"; sourceTree = "<group>"; };
2358A60B233DBB6D00623E4E /* YBNetworkResponse+YBCustom.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "YBNetworkResponse+YBCustom.m"; sourceTree = "<group>"; };
23730AE5227FDFCA0033BB11 /* DefaultServerRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DefaultServerRequest.m; sourceTree = "<group>"; };
23730AE6227FDFCA0033BB11 /* DefaultServerRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DefaultServerRequest.h; sourceTree = "<group>"; };
23730AE7227FDFCA0033BB11 /* OtherServerRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OtherServerRequest.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,6 +79,15 @@
name = Frameworks;
sourceTree = "<group>";
};
2358A606233DBB0900623E4E /* 为响应对象拓展属性 */ = {
isa = PBXGroup;
children = (
2358A60A233DBB6D00623E4E /* YBNetworkResponse+YBCustom.h */,
2358A60B233DBB6D00623E4E /* YBNetworkResponse+YBCustom.m */,
);
path = "为响应对象拓展属性";
sourceTree = "<group>";
};
23C918F7225C4039006D7572 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -159,6 +171,7 @@
children = (
23730AEC227FE0400033BB11 /* TestViewController.h */,
23730AEB227FE0400033BB11 /* TestViewController.m */,
2358A606233DBB0900623E4E /* 为响应对象拓展属性 */,
23C91934225C4BCD006D7572 /* APITeams */,
);
path = TestCase;
Expand Down Expand Up @@ -278,6 +291,7 @@
files = (
23C9192A225C4204006D7572 /* YBNetworkManager.m in Sources */,
23C91908225C4039006D7572 /* ViewController.m in Sources */,
2358A60C233DBB6D00623E4E /* YBNetworkResponse+YBCustom.m in Sources */,
23C91913225C403C006D7572 /* main.m in Sources */,
23730AEA227FDFCA0033BB11 /* OtherServerRequest.m in Sources */,
23730AED227FE0400033BB11 /* TestViewController.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "YBBaseRequest.h"
#import "YBNetworkResponse+YBCustom.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down
47 changes: 39 additions & 8 deletions YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ - (instancetype)init {
self = [super init];
if (self) {
self.baseURI = @"http://japi.juhe.cn";
self.requestSerializer = [AFHTTPRequestSerializer serializer];
self.requestSerializer.timeoutInterval = 25;
self.responseSerializer = [AFJSONResponseSerializer serializer];
NSMutableSet *types = self.responseSerializer.acceptableContentTypes.mutableCopy;
[types addObject:@"text/html"];
self.responseSerializer.acceptableContentTypes = types;

[self.cacheHandler setShouldCacheBlock:^BOOL(YBNetworkResponse * _Nonnull response) {
// 检查数据正确性,保证缓存有用的内容
Expand All @@ -33,14 +27,51 @@ - (instancetype)init {

#pragma mark - override

- (AFHTTPRequestSerializer *)requestSerializer {
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer new];
serializer.timeoutInterval = 25;
return serializer;
}

- (AFHTTPResponseSerializer *)responseSerializer {
AFHTTPResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
NSMutableSet *types = [NSMutableSet set];
[types addObject:@"text/html"];
[types addObject:@"text/plain"];
[types addObject:@"application/json"];
[types addObject:@"text/json"];
[types addObject:@"text/javascript"];
serializer.acceptableContentTypes = types;
return serializer;
}

- (void)start {
NSLog(@"%@", self.requestIdentifier);
NSLog(@"发起请求:%@", self.requestIdentifier);
[super start];
}

- (void)yb_redirection:(void (^)(YBRequestRedirection))redirection response:(YBNetworkResponse *)response {

// 处理错误的状态码
if (response.error) {
YBResponseErrorType errorType;
switch (response.error.code) {
case NSURLErrorTimedOut:
errorType = YBResponseErrorTypeTimedOut;
break;
case NSURLErrorCancelled:
errorType = YBResponseErrorTypeCancelled;
break;
default:
errorType = YBResponseErrorTypeNoNetwork;
break;
}
response.errorType = errorType;
}

// 自定义重定向
NSDictionary *responseDic = response.responseObject;
if ([responseDic isKindOfClass:NSDictionary.self] && [[NSString stringWithFormat:@"%@", responseDic[@"error_code"]] isEqualToString:@"2"]) {
if ([[NSString stringWithFormat:@"%@", responseDic[@"error_code"]] isEqualToString:@"2"]) {
redirection(YBRequestRedirectionFailure);
response.errorType = YBResponseErrorTypeServerError;
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// YBNetworkResponse+YBCustom.h
// YBNetworkDemo
//
// Created by 波儿菜 on 2019/9/27.
// Copyright © 2019 杨波. All rights reserved.
//

#import "YBNetworkResponse.h"

NS_ASSUME_NONNULL_BEGIN

/// 网络响应错误类型
typedef NS_ENUM(NSInteger, YBResponseErrorType) {
/// 未知
YBResponseErrorTypeUnknown,
/// 超时
YBResponseErrorTypeTimedOut,
/// 取消
YBResponseErrorTypeCancelled,
/// 无网络
YBResponseErrorTypeNoNetwork,
/// 服务器错误
YBResponseErrorTypeServerError,
/// 登录状态过期
YBResponseErrorTypeLoginExpired
};

@interface YBNetworkResponse (YBCustom)

/// 请求失败类型 (使用该属性做业务处理足够)
@property (nonatomic, assign) YBResponseErrorType errorType;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// YBNetworkResponse+YBCustom.m
// YBNetworkDemo
//
// Created by 波儿菜 on 2019/9/27.
// Copyright © 2019 杨波. All rights reserved.
//

#import "YBNetworkResponse+YBCustom.h"
#import <objc/runtime.h>

@implementation YBNetworkResponse (YBCustom)

static void const *YBNetworkResponseErrorTypeKey = &YBNetworkResponseErrorTypeKey;
- (void)setErrorType:(YBResponseErrorType)errorType {
objc_setAssociatedObject(self, YBNetworkResponseErrorTypeKey, @(errorType), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (YBResponseErrorType)errorType {
NSNumber *tmp = objc_getAssociatedObject(self, YBNetworkResponseErrorTypeKey);
return tmp.integerValue;
}

@end

0 comments on commit 7dd3ce0

Please sign in to comment.