2013-12-18 19 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

yong ho查看我的更新回覆 –

回答

1

你可以使用AFNetworking如果您下載數據

加入您的操作(在你的情況NSURLSessionDataTask)成NSOperationQueue和完成回調每次操作獲取 集maximumconcurrentoperationCount 1

下載的數據(操作結果)

示例代碼

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任務完成它?這是一個下載操作,不是我正在尋找的。 –