在循环内部运行中运行请求操作
如何在1请求的成功块内部运行多个请求并等待它完成?在循环内部运行中运行请求操作
[manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@ Response: \n%@", url, responseObject);
resultsArray = [[NSMutableArray alloc] init];
for (NSDictionary *json in [responseObject objectForKey:@"items"]) {
[self getDetails:json];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
}];
凡getDetails:(ID)JSON是加载的组,其参数是基于主请求的结果的请求的方法。
例如: 我想从API请求学生列表,然后在成功块上。对于每个学生,我想从另一个表(另一个请求)获取相关数据并将它们放在我的NSObject上。
EDIT这是我的getDetails方法
- (AFHTTPRequestOperation *)getDetails:(NSDictionary *)json
{
NSLog(@"Start Op %@",[json objectForKey:@"related_salon"]);
NSString *url = [NSString stringWithFormat:@"%@read/salons/%@",SERVER_API_URL,[json objectForKey:@"related_salon"]];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
//op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@",[json objectForKey:@"name"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed Op %@",error.localizedDescription);
}];
//AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:req];
//op.responseSerializer = [AFJSONResponseSerializer serializer];
[op start];
return op;
}
的AFNetworking GET
方法返回一个ATHTTPRequestOperation
(一个NSOperation
子类)。你可以让你的getDetails
方法返回该对象。然后,您可以创建依赖于这些操作,其中你会在结束时运行的新操作:
NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
// add here whatever you want to perform when all the getDetails calls are done,
// e.g. maybe you want to dismiss your HUD when all the requests are done.
[SVProgressHUD dismiss];
}];
[manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@ Response: \n%@", url, responseObject);
resultsArray = [[NSMutableArray alloc] init];
for (NSDictionary *json in [responseObject objectForKey:@"items"]) {
NSOperation *operation = [self getDetails:json];
[completionOperation addDependency:operation];
}
[[NSOperationQueue mainQueue] addOperation:completionOperation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
}];
再次,这是假设该getDetails
是做自己的GET
调用,您更改getDetails
到(一)捕获由GET
返回的NSOperation
和(b)返回它。
我的getDetails如何看起来像?我是否设置了AFHTTPRequestOperation并以该方法返回? – 2014-08-28 04:54:33
当然,您可以创建自己的AFHTTPRequestOperation并将其添加到队列中,或者执行相应的GET请求,它会为您创建一个AFHTTPRequestOperation,并将其添加到经理队列中并返回给您。 (对不起,我认为你已经写过这个方法了。)关键是,就像AFNetworking的GET方法将'AFHTTPRequestOperation'返回给'getDetails'方法一样,'getDetails'方法也应该返回上面的代码,所以你可以做一些事情,比如建立依赖关系。 – Rob 2014-08-28 05:02:05
我试过这种方法,但我得到的结果是: 操作1成功> 完成操作完成> 操作2成功 请参阅我的方法getDetails编辑的问题。 – 2014-08-29 04:45:33
我不确定这一点,因为我还没有真正的连续运行操作。为什么不在其操作队列中使用AFHTTPRequestOperationManager和队列操作,为后续操作添加依赖关系?这**可能会给你串行执行(再次,不确定)。 – n00bProgrammer 2014-08-28 06:37:13