user2057435
user2057435

Reputation: 11

How can I use multiple AFJSONRequestOperations and combine the results into a single NSDictionary?

I'm not sure if I have two problems here or just one. I'm trying to download multiple JSON files from the Google Calendar API and combine all the results into a single NSDictionary or NSMutableDictionary.

client = [[AFHTTPClient alloc] init];
[client.operationQueue setMaxConcurrentOperationCount:1];

eventData = [NSMutableDictionary dictionary];
NSMutableArray *operations = [[NSMutableArray alloc] init];
for (int i = 0; i < calendars.count; i++){
    NSString *calendarID = [calendars objectAtIndex:i];
    NSString *urlString = [NSString stringWithFormat:@"https://www.googleapis.com/calendar/v3/calendars/%@/events?key=%@&singleEvents=true&orderBy=startTime",calendarID,apiKey];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSDictionary *add = JSON;
        [eventData addEntriesFromDictionary:add];
        if (i == calendars.count-1) {
            [self parseData];
        }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"AFJSONRequest failed with request: %@, and with error: %@",request, error);
    }];

    [operations addObject:operation];
}
[client enqueueBatchOfHTTPRequestOperationsWithRequests:operations progressBlock:nil completionBlock:^(NSArray *operations) {
    NSLog(@"DONE");
}];

The AFJSONRequestOperations in this code never executes. I've had some luck getting them to execute by placing them in an NSOperationQueue instead of the AFHTTPClient en batch, but I don't have all the data in one dictionary, only the last executed one. Plus I don't have the nice progress and completion blocks like on the AFHTTPClient that I assumed I could use like this.

Would getting the AFHTTPClient operationQueue to work also fix the problem of combining multiple dictionaries asynchronously?

Upvotes: 1

Views: 170

Answers (1)

JuJoDi
JuJoDi

Reputation: 14965

use

[operation start];

To start the AFJSONRequestOperation

Upvotes: 0

Related Questions