#pragma mark - getter
- (NSOperationQueue *)queue
{
if (!_queue) {
_queue = [[NSOperationQueuealloc]init];
}
return_queue;
}
这里是我个人对AFN的一个封装类,后面会说到
NSOperationQueue *mainQueue = [NSOperationQueuemainQueue];
//创建网络请求对象
RequestManager *manager = [RequestManagermanager];
// 配置请求参数
FSDynamicParams *dynamicParams = [[FSDynamicParamsalloc]initWithDynamicListParamsWithCityId:@2page:self.page];
// 这里对的AFHTTPRequestOperation请求进行了封装,目的是让代码更加简洁
AFHTTPRequestOperation *operationList = [manageroperationGETWithParams:dynamicParamsToController:selfSuccessHandle:^(id responseObject) {
FSLog(@"第一步");
}];
//配置请求参数
FSDynamicParams *messageParams = [[FSDynamicParamsalloc]initWithDynamicMessageParams];
// 这里对的AFHTTPRequestOperation请求进行了封装,目的是让代码更加简洁
AFHTTPRequestOperation *operationMessage = [manageroperationGETWithParams:messageParamsToController:selfSuccessHandle:^(id responseObject) {
FSLog(@"第二步");
}];
NSBlockOperation *operation3 = [NSBlockOperationblockOperationWithBlock:^{
#warning 这里好奇怪,一定要开一个延时线程才能按正常顺序执行,AFN线程的坑真是多,记录一下
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
FSLog(@"第三步");
});
}];
[operation3addDependency:operationList];
[operation3addDependency:operationMessage];
[self.queueaddOperation:operationList];
[self.queueaddOperation:operationMessage];
[mainQueueaddOperation:operation3];
说一下封装类的,RequestManager的部分代码
@class BaseParams;
@class FSBaseViewController;
@interface RequestManager :NSObject
- (AFHTTPRequestOperation *)operationGETWithParams:(BaseParams *)params ToController:(UIViewController *)controller SuccessHandle:(void (^)(id responseObject))SuccessHandle;
@end
@implementation RequestManager
+ (instancetype)manager
{
RequestManager *manager = [[RequestManageralloc]init];
return manager;
}
// 返回线程操作
- (AFHTTPRequestOperation *)operationGETWithParams:(BaseParams *)params ToController:(UIViewController *)controller SuccessHandle:(void (^)(id responseObject))SuccessHandle{
//将字典的参数通过AFN的方法转化成http结构的字符串
NSMutableURLRequest *request = [[AFHTTPRequestSerializerserializer]requestWithMethod:@"GET"URLString:URL_BASEparameters:[paramskeyValue]error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];
// 设置返回解析器为JSON格式
operation.responseSerializer = [AFJSONResponseSerializerserializer];
// 设置线程操作
[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject) {
if ([responseObject[@"result"]intValue] !=0 ) {
// 这里是对MBProgressHUD的再封装,自定义了一个类
[MBProgressHUBhubWithShowText:responseObject[@"message"]toView:controller.view];
return;
}
FSLog(@"success:%@",responseObject);
SuccessHandle(responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
//请求失败后的统一操作,例如告诉用户,请求超时来,,什么什么的,传进来的控制器可以在这里判断,改控制器为哪一个时可以做一些显示不同图片的操作
//网络请求层,就应该做网络请求的处理嘛,个人是这么认为了,网络请求任何情况都归这个类管
if ([controllerisKindOfClass:[FSBaseTableViewControllerclass]]) {
[[(FSBaseTableViewController *)controllertableView].headerendRefreshing];
}
[MBProgressHUBhubWithShowStatusCode:NetWorkNotRechabilytoView:controller.view];
FSLog(@"failure:request --- %@",operation.request);
}];
return operation;
}