我不确定我在这里遇到两个问题还是只有一个问题。我正在尝试从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 en批处理中,我有幸运行它们,但我没有一个字典中的所有数据,只有最后执行的数据。另外,我没有像AFHTTPClient那样的好的进度和完成块,我认为我可以这样使用。
让AFHTTPClient operationQueue工作也能解决异步组合多个字典的问题吗?
答案 0 :(得分:0)
使用
[operation start];
启动AFJSONRequestOperation