dispatch_semaphore_t signal = dispatch_semaphore_create(0);
dask = [NetWorkManager PostWithUrl:KAPI_RCIM_ConversationRelation Params:@{@"token":[UserManager share].userModel.token, @"to_user_id": modelIDString} successHander:^(id reponse) {
if ([reponse[@"code"] integerValue] == 0) {
dispatch_semaphore_signal(signal);
}else {
dispatch_semaphore_signal(signal);
}
} returnFailHander:^(id returnFail) {
dispatch_semaphore_signal(signal);
} failHander:^(NSError *error) {
dispatch_semaphore_signal(signal);
}];
dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);
return someThing;
最近发现不好使了,因为AFN把回调放在主线程了
需要设置一下manager的线程:
manager.completionQueue = dispatch_get_global_queue(0, 0);
dispatch_semaphore_t signal = dispatch_semaphore_create(0);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/html", @"text/json", nil];
manager.requestSerializer.timeoutInterval = 30;
manager.completionQueue = dispatch_get_global_queue(0, 0);
[manager POST:@"http://www.test.com/" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_semaphore_signal(signal);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(signal);
}];
dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);
而且不要在afn回调的block里进行UI修改,会报这个错:
Main Thread Checker: UI API called on a background thread
把更新UI操作放到dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);后面就可以了