我试图实现的是向服务器发送两个请求,这些请求很重要,并且在第一个请求结果返回之前,第二个请求的参数是未知的。
我已经使用 afnetworking2.0 作为以下代码片段
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
NSMutableURLRequest*request=[NSMutableURLRequest requestWuthURL@"URLSTRING"];
// configure the request with parameters
[request setHTTPBody:JsonData];
[request setHTTPMethod:@"POST"];
AFHTTPRequestOperation* operation=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setComplemetionBlockWithSucess:^(AFHTTPRequestOperation* operation, id responsObject)
{
//parse the result using NSXMLParser;
NSInteger result=weakSelf.parseRule.identifier;
}
failure :^(AFHTTPRequestOperation* operation, NSError *error)
{
NSLog(@"fail");
}];
NSMutableURLRequest*secondRequest=[NSMutableURLRequest requestWuthURL@"URLSTRING"];
//Using the first request result to set the parameters in second request
[secondRequest setHTTPBody:JsonData];
[secondRequest setHTTPMethod:@"POST"];
AFHTTPRequestOperation* secondOperation=[[AFHTTPRequestOperation alloc]initWithRequest:secondRequest];
[secondOperation setComplemetionBlockWithSucess:^(AFHTTPRequestOperation* operation, id responsObject)
{
//do something
}
failure :^(AFHTTPRequestOperation* operation, NSError *error)
{
NSLog(@"fail");
}];
[secondOperation addDependency:Operation];
[queue addOPerations:@[operation,secondOperation]];
哪个不起作用,我可以正确地取回第一个操作结果,但问题是我的第二个请求参数设置是在结果返回之前执行的。任何建议将不胜感激。我应该使用 dispatch_semaphore 吗?还是有其他建议?