在操作内部循环运行请求操作 [英] Run request operations in loop inside operation
问题描述
如何在1个请求的成功块中运行多个请求并等待其完成?
How can I run multiple requests inside the success block of 1 request and wait for it to finish?
[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是加载参数基于的请求组的方法
Where in getDetails:(id)json is the method to load the group of requests whose parameters are based on the result of the main request.
例如:
我想从API请求一个学生名单,然后在成功块上。对于每个学生,我想从另一个表(另一个请求)中获取相关数据,并将其放在我的NSObject上。
For example: I would like to request from the API a list of students, then on the success block. For each student, I would like to get the related data from another table (another request) and put them on my NSObject.
编辑是我的getDetails方法
EDIT Here is my getDetails method
- (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
方法返回该对象。然后,您可以根据这些操作创建一个新的操作,该操作将在最后运行:
The AFNetworking GET
method returns a ATHTTPRequestOperation
(an NSOperation
subclass). You could have your getDetails
method return that object. You can then create a new operation dependent upon those operations, which you'd run at the end:
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
更改为(a)捕获 NSOperation
由 GET返回的
,然后(b)返回。
Again, this is presuming that getDetails
is doing its own GET
call, and that you change getDetails
to (a) capture the NSOperation
returned by GET
and (b) return it.
这篇关于在操作内部循环运行请求操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!