如何在iOS中执行块后立即获得响应?

时间:2015-10-27 11:30:00

标签: ios objective-c asynchronous afnetworking block

我使用AFNetworking框架来调用Web服务,我已经使用过了 POST方法从Web服务获取响应:

       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
               manager.requestSerializer = [AFJSONRequestSerializer serializer];
               [manager.requestSerializer setTimeoutInterval:25];
               [manager POST:stringURL parameters:param
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                   NSLog(@"RESPONCE : %@", responseObject);
                 } 
              failure:^(AFHTTPRequestOperation *operation, NSError *error) 
                {
               NSLog(@"Error: %@", error);
               NSString *msg = error.localizedDescription;
               UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"That Shift" message:msg delegate:nil cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil, nil];
              [alertview show];
                                  }];
        NSLog(@"Right AFter Block Execution RESPONCE : %@", responseObject);

我希望我的回复在NSLog(@"Right AFter Block Execution RESPONCE : %@", responseObject);执行后立即可以任何人建议我该怎么做?

原因是BEHINDE:

这样做的原因是我想在我给出的代码之后使用另一个块,我想在第二个块中使用该响应,这将在NSLog(@"Right AFter Block Execution RESPONCE : %@", responseObject);的plce之后。 / p>

1 个答案:

答案 0 :(得分:0)

您可以使用信号量来同步线程,因此第一个线程将阻塞并等待第二个线程。但是不要在主线程上执行它,因为你的GUI会冻结。

dispatch_semaphore_t sema = dispatch_semaphore_create(0);
// Inside callback:
 dispatch_semaphore_signal(sema);
// Before NSLOG
dispatch_semaphore_wait(sema, <timeout_may_be_forever>);