面试题答案
一键面试1. 数据结构设计
- 请求配置结构体 定义一个结构体来存储每个请求的相关配置信息,包括请求类型、超时时间、重试策略等。
typedef NS_ENUM(NSUInteger, RequestType) {
RequestTypeGET,
RequestTypePOST,
RequestTypePUT
};
typedef NS_ENUM(NSUInteger, RetryStrategy) {
RetryStrategyFixed,
RetryStrategyExponentialBackoff
};
typedef struct {
RequestType type;
NSTimeInterval timeout;
RetryStrategy retryStrategy;
NSInteger maxRetryCount;
} RequestConfiguration;
- 请求任务类 创建一个类来封装网络请求任务,这个类需要保存请求配置、当前重试次数等信息。
@interface NetworkRequestTask : NSObject
@property (nonatomic, strong) NSURLSessionDataTask *dataTask;
@property (nonatomic, assign) NSInteger retryCount;
@property (nonatomic, assign) RequestConfiguration configuration;
- (instancetype)initWithConfiguration:(RequestConfiguration)configuration;
- (void)startRequest;
@end
2. 关键方法设计
- 网络请求方法
在
NetworkRequestTask
类中实现网络请求的具体逻辑,包括设置超时时间、发起请求等操作。
@implementation NetworkRequestTask
- (instancetype)initWithConfiguration:(RequestConfiguration)configuration {
self = [super init];
if (self) {
_configuration = configuration;
_retryCount = 0;
}
return self;
}
- (void)startRequest {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.timeoutIntervalForRequest = _configuration.timeout;
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 根据请求类型构建请求
NSMutableURLRequest *request = [NSMutableURLRequest new];
switch (_configuration.type) {
case RequestTypeGET:
request.HTTPMethod = @"GET";
break;
case RequestTypePOST:
request.HTTPMethod = @"POST";
break;
case RequestTypePUT:
request.HTTPMethod = @"PUT";
break;
}
_dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
if (_retryCount < _configuration.maxRetryCount) {
_retryCount++;
[self startRequest];
} else {
// 处理重试次数用尽的情况
}
} else {
// 处理成功响应
}
}];
[_dataTask resume];
}
@end
- 重试策略方法
可以在
NetworkRequestTask
类中添加方法来处理不同的重试策略。
- (NSTimeInterval)nextRetryInterval {
switch (_configuration.retryStrategy) {
case RetryStrategyFixed:
return 1.0; // 固定重试间隔1秒
break;
case RetryStrategyExponentialBackoff:
return pow(2, _retryCount); // 指数退避策略
break;
}
return 0;
}
3. 通用框架整合
- 请求管理类 创建一个请求管理类来管理所有的网络请求任务,负责创建请求任务、调度请求等。
@interface NetworkRequestManager : NSObject
@property (nonatomic, strong) NSMutableArray<NetworkRequestTask *> *tasks;
+ (instancetype)sharedManager;
- (void)addRequestWithConfiguration:(RequestConfiguration)configuration;
@end
@implementation NetworkRequestManager
+ (instancetype)sharedManager {
static NetworkRequestManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[NetworkRequestManager alloc] init];
manager.tasks = [NSMutableArray new];
});
return manager;
}
- (void)addRequestWithConfiguration:(RequestConfiguration)configuration {
NetworkRequestTask *task = [[NetworkRequestTask alloc] initWithConfiguration:configuration];
[self.tasks addObject:task];
[task startRequest];
}
@end
通过以上设计,可以构建一个通用的框架来处理不同类型网络请求的超时与重试。在实际应用中,可以根据具体需求进一步优化和扩展,例如添加请求队列管理、请求优先级等功能。