1

我不确定我这里有两个问题还是只有一个。我正在尝试从 Google Calendar API 下载多个 JSON 文件并将所有结果合并到一个 NSDictionary 或 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");
}];

此代码中的 AFJSONRequestOperations 永远不会执行。通过将它们放置在 NSOperationQueue 而不是 AFHTTPClient 批处理中,我有一些运气让它们执行,但是我没有一个字典中的所有数据,只有最后执行的一个。另外,我没有我认为可以像这样使用的 AFHTTPClient 上的良好进度和完成块。

让 AFHTTPClient operationQueue 工作也能解决异步组合多个字典的问题吗?

4

1 回答 1

0

利用

[operation start];

启动 AFJSONRequestOperation

于 2013-09-07T22:10:46.347 回答