2

NSURLSessionDataTask を使用して一連のリクエストを実行します。最初のリクエストが終了したら、最初のリクエストの responseData を使用して別の複数のリクエストを実行する必要があります。最後に、NSArray を取得し、テーブル ビューに渡します。どうやってするか?以下に示すように、機能していません。

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
NSString *tvdbId = [[NSUserDefaults standardUserDefaults] objectForKey:@"tvdbId"];
NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.trakt.tv/show/seasons.json/%@/%@", kApiKey, tvdbId]];
__weak EpisodeViewController *weakSelf = self;
NSURLSessionDataTask *task = [manager dataTaskWithRequest:[NSURLRequest requestWithURL:urlString] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (!error) {
        NSArray *seasons = (NSArray *)responseObject;
        __block NSMutableArray *seasonArray = [NSMutableArray new];

        [seasons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSString *seasonNumber = obj[@"season"]; 
            NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.trakt.tv/show/season.json/%@/%@/%@", kApiKey, tvdbId, seasonNumber]];
            NSURLSessionDataTask *eposideTask = [manager dataTaskWithRequest:[NSURLRequest requestWithURL:urlString] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                NSArray *eposides = (NSArray *)responseObject;
                NSDictionary *dict = @{@"season": seasonNumber, @"eposodes": eposides};
                [seasonArray addObject:dict];
            }];
            [eposideTask resume];
        }];
        weakSelf.eposides = [NSArray arrayWithArray:seasonArray];
        NSLog(@"%@", weakSelf.eposides);

    }
}];
[task resume];
4

1 に答える 1