ios - 使用 NSURLSessionDataTask 的链式请求
<p><p>我将使用 NSURLSessionDataTask 执行一个请求链。当第一个请求完成后,我需要使用第一个请求中的 responseData 来执行另一个多重请求。最后,我得到了 NSArray 并交给了表格 View 。怎么做?正如您在下面看到的,它不起作用。</p>
<pre><code>NSURLSessionConfiguration *config = ;
AFURLSessionManager *manager = [ initWithSessionConfiguration:config];
NSString *tvdbId = [ objectForKey:@"tvdbId"];
NSURL *urlString = ];
__weak EpisodeViewController *weakSelf = self;
NSURLSessionDataTask *task = completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (!error) {
NSArray *seasons = (NSArray *)responseObject;
__block NSMutableArray *seasonArray = ;
[seasons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *seasonNumber = obj[@"season"];
NSURL *urlString = ];
NSURLSessionDataTask *eposideTask = completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSArray *eposides = (NSArray *)responseObject;
NSDictionary *dict = @{@"season": seasonNumber, @"eposodes": eposides};
;
}];
;
}];
weakSelf.eposides = ;
NSLog(@"%@", weakSelf.eposides);
}
}];
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以使用 <a href="https://github.com/AFNetworking/AFNetworking" rel="noreferrer noopener nofollow">AFNetworking</a>如果您正在下载数据</p>
<p>将您的操作(在您的情况下为 NSURLSessionDataTask)添加到 NSOperationQueue 和
将最大并发操作计数设置为 1 </p>
<p>从每个操作的Completion回调中获取下载的数据(操作结果)</p>
<p>示例代码</p>
<pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [ stringByAppendingPathComponent:];
NSURLRequest *request = ];
operation = [ initWithRequest:request targetPath:path shouldResume:YES];
operation.outputStream = ;
[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);
}];
[ addOperation:operation];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 NSURLSessionDataTask 的链式请求,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20653695/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20653695/
</a>
</p>
页:
[1]