如何在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;
}
我不確定這一點,因爲我還沒有真正的連續運行操作。爲什麼不在其操作隊列中使用AFHTTPRequestOperationManager和隊列操作,爲後續操作添加依賴關係?這**可能會給你串行執行(再次,不確定)。 – n00bProgrammer 2014-08-28 06:37:13