具有彼此依赖性的多个AFHTTPRequestOperations

时间:2013-10-31 00:06:17

标签: concurrency nsoperation nsoperationqueue afnetworking-2

我想要实现的是向服务器发送两个请求,其中订单很重要,第二个请求的参数在第一个请求结果返回之前是未知的。

我已经使用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吗?还是其他任何建议?

0 个答案:

没有答案