Run request operations in loop inside operation

女生的网名这么多〃 提交于 2019-12-12 02:26:08

问题


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]; 
}];

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.

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.

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;
}

回答1:


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]; 
}];

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.



来源:https://stackoverflow.com/questions/25540749/run-request-operations-in-loop-inside-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!