如要查看完整代码,请移步仿煎蛋github
1、几个要点
+ (AFHTTPRequestOperationManager *)sharedHTTPOperationManager {
static AFHTTPRequestOperationManager *manager = nil;
static dispatch_once_t pred;
dispatch_once(&pred,^{
manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer new];
manager.requestSerializer.timeoutInterval = 30.f;
});
return manager;
}
- block形式的结果转换成信号(参考AFNetworking-RACExtensions)
/** * 转换成响应式请求 可重用 * * @param url 请求地址 * @param params 请求参数 * * @return 带请求结果(字典)的信号 */
+ (RACSignal *)post2racWthURL:(NSString *)url params:(NSDictionary *)params {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
AFHTTPRequestOperationManager *manager = [self sharedHTTPOperationManager];
AFHTTPRequestOperation *operation= [manager POST:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation,id _Nonnull responSEObject) {
[self handleResultWithSubscriber:(id<RACSubscriber>)subscriber operation:operation responSEObject:responSEObject];
} failure:^(AFHTTPRequestOperation * _Nonnull operation,NSError * _Nonnull error) {
NSMutableDictionary *userInfo = [error.userInfo mutableCopy] ?: [NSMutableDictionary dictionary];
userInfo[RAFNetworkingOperationErrorKey] = operation;
userInfo[RAFNetworkingErrorInfoKey] = [self handleErrorMessage:error];
[subscriber sendError:[NSError errorWithDomain:error.domain code:error.code userInfo:userInfo]];
}];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}];
}
- 字典转模型(使用了MJ老师的MJExtension框架)
/** * 响应式post请求 返回处理后的结果 对象类型 可重用 * * @param url 请求地址 * @param params 请求参数 * @param clazz 字典对应的对象 * * @return 带请求结果(对象)的信号 */
+ (RACSignal *)racPOSTWithURL:(NSString *)url params:(NSDictionary *)params class:(Class)clazz {
return [[[self post2racWthURL:url params:params] map:^id(id responSEObject) {
return [clazz objectWithKeyValues:responSEObject];
}] replayLazily];
}
- 错误处理(1,没有正确返回结果 2,返回结果中带有错误)
+ (NSString *)handleErrorMessage:(NSError *)error {
NSString *result = nil;
switch (error.code) {
case kcfURLErrorTimedOut:
result = @"服务器连接超时";
break;
case kcfURLErrorNotConnectedToInternet:
case kcfURLErrorCannotDecodeContentData:
result = @"您连接的网络不正常,请检查您的网络连接";
break;
case kcfURLErrorCannotFindHost:
result = @"服务器内部错误";
break;
default:
LogBlue(@"其他错误 error:%@",error);
break;
}
return result;
}
+ (void)handleResultWithSubscriber:(id<RACSubscriber>)subscriber operation:(AFHTTPRequestOperation * _Nonnull)operation responSEObject:(NSDictionary * _Nonnull)responSEObject{
NSString *status=[responSEObject objectForKey:@"status"];
if ([status isEqualToString:@"ok"]) {
[subscriber sendNext:responSEObject];
[subscriber sendCompleted];
}else {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
BOOL isError=[status isEqualToString:@"error"];
userInfo[RAFNetworkingErrorInfoKey] =isError?[responSEObject objectForKey:@"error"]:@"请求没有得到处理";
userInfo[RAFNetworkingOperationErrorKey] = operation;
NSError *error= error=[NSError errorWithDomain:customDomain code:isError?AFNetWorkUtilsResponseError
:AFNetWorkUtilsResponsePending userInfo:userInfo];
[subscriber sendError:error];
}
}
2、封装的目的
原文链接:https://www.f2er.com/react/307796.html