2014-08-28 40 views
0

如何在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; 
} 
+0

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

回答

0

的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)返回它。

+0

我的getDetails如何看起來像?我是否設置了AFHTTPRequestOperation並以該方法返回? – 2014-08-28 04:54:33

+0

當然,您可以創建自己的AFHTTPRequestOperation並將其添加到隊列中,或者執行相應的GET請求,它會爲您創建一個AFHTTPRequestOperation,並將其添加到經理隊列中並返回給您。 (對不起,我認爲你已經寫過這個方法了。)關鍵是,就像AFNetworking的GET方法將'AFHTTPRequestOperation'返回給'getDetails'方法一樣,'getDetails'方法也應該返回上面的代碼,所以你可以做一些事情,比如建立依賴關係。 – Rob 2014-08-28 05:02:05

+0

我試過這種方法,但我得到的結果是: 操作1成功> 完成操作完成> 操作2成功 請參閱我的方法getDetails編輯的問題。 – 2014-08-29 04:45:33