首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSURLSessionDataTask的链式请求

使用NSURLSessionDataTask的链式请求
EN

Stack Overflow用户
提问于 2013-12-18 09:05:41
回答 1查看 1.6K关注 0票数 2

我将使用NSURLSessionDataTask执行一个请求链。当第一个请求完成时,我需要使用来自第一个请求的responseData来执行另一个多个请求。最后,我得到了NSArray并提供给表视图。怎么做?正如你在下面看到的,它不起作用。

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-18 09:21:11

如果您正在下载数据,可以使用AFNetworking

将操作(在本例中为NSURLSessionDataTask)添加到NSOperationQueue中,并将maximumconcurrentoperationCount设置为1

从每个操作的完成调用开始,获取下载的数据(操作结果)。

样本码

代码语言:javascript
复制
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];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20653695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档