ios - 如何制作排队系统?

标签 ios objective-c

下面我有一些下载视频的代码。该代码从我提供的字符串中下载视频,每次通话只能下载一个视频。我的问题是我可以制作一个排队系统,可以同时启动下一个提供的字符串。 提前致谢

 -(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/

相关文章:

ios - iOS 9 中的 PKContact 失踪了

iphone - 请帮助调试 UILabel 上的崩溃 (evict_glyph_entry_from_cache)

ios - 更改 UITableViewCell 样式会导致单击操作不会发生

ios - 如何衡量应用的数据流量?

ios - 为扩展 View 添加委托(delegate)和数据源

ios - 使按钮从数组中读取字符串

iphone - 释放 UIImage 会使应用程序崩溃

ios - keyWindow 并不总是返回 UIWindow

ios - 解析 JSON 并保存在本地

ios - 在 iOS 中创建另一个 View 的图像