下面我有一些下载视频的代码。该代码从我提供的字符串中下载视频,每次通话只能下载一个视频。我的问题是我可以制作一个排队系统,可以同时启动下一个提供的字符串。 提前致谢
-(IBAction)download{
LBYouTubeExtractor *extractor = [[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:URL.text] quality:LBYouTubeVideoQualityLarge];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
if(!error) {
NSLog(@"Did extract video URL using completion block: %@", videoURL);
NSURL *url = videoURL;
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
if(!error) {
NSLog(@"Did extract video URL using completion block: %@", videoURL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
filePath = [NSString stringWithFormat:@"%@/Documents/%@.mp4", NSHomeDirectory(),title.text];
filename = [NSString stringWithFormat:(@"video_%@.mp4"), videoURL];
data = [NSData dataWithContentsOfURL: videoURL];
[data writeToFile:filePath atomically:YES];
NSLog(@"File %@ successfully saved", filePath);
}
);
}
else {
NSLog(@"Failed extracting video URL using block due to error:%@", error);
}
}];
} else {
NSLog(@"Failed extracting video URL using block due to error:%@", error);
}
}];
}
最佳答案
看看NSOperationQueue .
用 maxConcurrentOperationCount 创建一个 NSOperationQueue 是相当简单的的 1,并传递您的队列代码块以使用 addOperationWithBlock 执行.在您的情况下,您可以遍历视频列表并将每个下载排队到您的 NSOperationQueue。
关于ios - 如何制作排队系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574083/