AFNetWorking同步请求

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); //创建信号量
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 5;
manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//如果之前请求有值,则异步就好,不然会卡主线程
if([OverallModel shareOverallModel].nSystemParameter){
dispatch_semaphore_signal(semaphore);
}
[manager GET:[AppURLRequest getSystemVekey] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) { dispatch_semaphore_signal(semaphore);//不管请求状态是什么,都得发送信号,否则会一直卡着进程
NSInteger result = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result"]]integerValue];
if(result == 1){
NSString *nSystemParameter = NSZL(responseObject[@"data"][@"nSystemParameter"]);
NSString *QQInfo = NSZL(responseObject[@"data"][@"QQInfo"]);
[OverallModel shareOverallModel].nSystemParameter = nSystemParameter;
[OverallModel shareOverallModel].QQInfo = QQInfo;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(semaphore);//不管请求状态是什么,都得发送信号,否则会一直卡着进程
}]; dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER); //等待

如果是在异步线程请求,则去掉 manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

上一篇:学习Android过程中遇到的未解决问题(个人笔记,细节补充,随时更新)


下一篇:CentOS之Vim