1

我不確定我是否有兩個問題,或者只有一個問題。我試圖從Google Calendar API下載多個JSON文件,並將所有結果合併到一個NSDictionary或NSMutableDictionary中。如何使用多個AFJSONRequestOperations並將結果合併到一個NSDictionary中?

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的工作是否也解決了異步合併多個字典的問題?

回答

0

使用

[operation start]; 

要啓動AFJSONRequestOperation

相關問題