2013-12-18 3 views
2

NSURLSessionDataTask를 사용하여 요청 체인을 실행하려고합니다. 첫 번째 요청이 끝나면 주먹 요청의 responseData를 사용하여 다른 여러 요청을 실행해야합니다. 그리고 결국, NSArray를 얻고 테이블 뷰로 이동합니다. 그렇게하는 방법? 아래에서 볼 수 있듯이 작동하지 않습니다. 당신이NSURLSessionDataTask를 사용한 체인 요청

데이터를 다운로드하는 경우

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]; 
+0

내 업데이트 확인 내 질문에 답할 –

답변

1

당신은 NSOperationQueue 다시 각 작업의 취득 완료 호출에서 1

에 설정 maximumconcurrentoperationCount로 (귀하의 경우 NSURLSessionDataTask에) 당신의 작업을 추가 AFNetworking를 사용 할 수 있습니다 다운로드 된 데이터 (작업 결과)

샘플 코드

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"temp.zip"]]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:TempUrl]]; 
    operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES]; 

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 

    [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 

     /// Check Download Progress   
     } 
    }]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    //// Success code goes here 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     NSLog(@"Error: %@", error); 

    }]; 

    [[self downloadQueue] addOperation:operation]; 
+0

예를 들어 줄 수 있습니까? –

+0

@yong ho 업데이트 확인 –

+1

NSURLSession 작업을 수행하는 방법은 무엇입니까? 그것은 내가 바라는 것이 아니라 다운로드 작업입니다. –